我一直在尝试(添加然后)从主屏幕中删除我的应用程序的快捷方式。添加快捷方式效果很好,但是我无法删除使用以下代码创建的快捷方式。
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" /
来了:
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" />
找出
了根本原因:-)
我想出了它对我不起作用的原因。我在手机上使用不同的第三方启动器(股票安卓启动器除外)。只要您使用的启动器支持该操作,创建和删除App-Shortcut
就可以。我在默认启动器上运行了上面的代码,它的工作方式就像一个魅力:)
谢谢大家的回复!