在Android中,是否可以检查应用程序快捷方式是否已存在?
谢谢, 罗斯汉
答案 0 :(得分:3)
你无法发现那种事情。
但是如果你需要在主屏幕上添加一个快捷方式,你可能会做类似的事情:
卸载现有快捷方式(如果有)
intent.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(intent);
安装新的快捷方式
intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(intent);
不要忘记在你的Manifest中添加此权限:
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT"/>
答案 1 :(得分:2)
// Checking if ShortCut was already added
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
boolean shortCutWasAlreadyAdded = sharedPreferences.getBoolean("PREF_KEY_SHORTCUT_ADDED", false);
if (shortCutWasAlreadyAdded) return;
Intent shortcutIntent = new Intent(getApplicationContext(), MainActivity.class);
shortcutIntent.setAction(Intent.ACTION_MAIN);
Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "SBM");
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.ic_launcher));
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
sendBroadcast(addIntent);
// Remembering that ShortCut was already added
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("PREF_KEY_SHORTCUT_ADDED", true);
editor.commit();