Android:如何从主屏幕删除/卸载应用程序快捷方式?
我一直在尝试(添加然后)从主屏幕删除我的应用程序的快捷方式。 添加快捷方式非常有效,但是我无法删除使用以下代码创建的快捷方式Android:如何从主屏幕删除/卸载应用程序快捷方式?,android,Android,我一直在尝试(添加然后)从主屏幕删除我的应用程序的快捷方式。 添加快捷方式非常有效,但是我无法删除使用以下代码创建的快捷方式 public void setupShortCut(boolean create) { shortcutIntent = new Intent(); shortcutIntent.setClassName("com.abc.xyz", "XYZActivity"); shortcutIntent.addFlags(Inten
public void setupShortCut(boolean create) {
shortcutIntent = new Intent();
shortcutIntent.setClassName("com.abc.xyz", "XYZActivity");
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
icon = Intent.ShortcutIconResource.fromContext(this, R.drawable.icon);
Intent intentShortcut = new Intent();
intentShortcut.putExtra("android.intent.extra.shortcut.INTENT", shortcutIntent);
intentShortcut.putExtra("android.intent.extra.shortcut.NAME", getResources().getString(R.string.app_name));
intentShortcut.putExtra("android.intent.extra.shortcut.ICON_RESOURCE", icon);
if(create) {
intentShortcut.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
} else {
intentShortcut.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
}
sendBroadcast(intentShortcut);
}
请告诉我哪里出了问题
编辑1:
我在清单文件中拥有所需的权限:
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" /
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
给你:
private void deleteShortCut(Context context) {
Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
shortcutIntent.setClassName("com.example.androidapp", "SampleIntent");
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
shortcutIntent.putExtra("someParameter", "HelloWorld");
Intent removeIntent = new Intent();
removeIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
removeIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "ShortcutName");
removeIntent.putExtra("duplicate", false);
removeIntent
.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
context.sendBroadcast(removeIntent);
}
要删除快捷方式,请尝试使用以下代码
final Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
shortcutIntent.setComponent(new ComponentName(this.getPackageName(), "YourClassName"));
final Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
intent.setComponent(new ComponentName(this.getPackageName(), "YourClassName"));
intent.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
sendBroadcast(intent, null);
向清单文件添加以下权限:
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" /
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
找出了根本原因:-)
我找到了它不适合我的原因。
我在手机上使用不同的第三方启动器(除了股票android启动器)。
创建和删除应用程序快捷方式
只要您使用的启动器支持该操作,就可以正常工作。
我在默认启动器上运行了上面的代码,它就像一个符咒:)
谢谢大家的回复 您在清单中是否有“com.android.launcher.permission.UNINSTALL_SHORTCUT”
权限?是的,我在清单中有此权限。您是否绝对肯定当您尝试删除快捷方式时调用的是setupShortCut(false)
?我绝对肯定这一点。我已经调试了代码,可以看到控件在else
中运行。Devangi,感谢您的回复,但是您的代码与我发布的代码非常相似,只有一个例外,即:RemoveContent.putExtra(“重复”,false)代码>我已将此代码添加到我的意图中,但它仍然不起作用。请尝试此链接。这是一个第三方启动器,限制我卸载我创建的应用程序快捷方式。我已经编辑了我的问题。谢谢你的代码,但是这段代码不适用于api级别21以上的版本。那么你找到解决方案了吗?谢谢你的回复,但是仍然不起作用。这对你有用吗?还是没办法。快捷方式有一个问题
-为了使用intent删除它们,你必须按照创建它们时保持的方式设置intent的属性。是第三方启动器限制我卸载我创建的应用程序快捷方式。我已经编辑了我的问题。是否可以删除应用程序的所有快捷方式,而不告诉setComponent每个快捷方式的类名?另外,为什么需要添加名称?真的需要吗?哪个发射器有问题?我明白了。它是否仍然可以与库存发射器配合使用?即使在较新的Android版本(棒棒糖,棉花糖,N)?@Android开发者:遗憾的是,我既没有访问应用程序(具有此功能),也没有尝试棒棒糖,棉花糖或N的代码。我在两年多前就尝试过这样做。