我在应用程序启动时在主屏幕上创建了应用程序的快捷方式,并且它在所有设备上运行良好
但是,在Nexus标签上,它会在每次启动应用时创建多个快捷方式?
如何在App安装时创建一个Shortcut,并在App上卸载它?
private void AddShortcutIconToHomeScreen() {
Intent shortcutIntent = new Intent(this, EmployeeListActivity.class);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Device Tracker");
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(getApplicationContext(),
R.drawable.ic_launcher));
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
addIntent.putExtra("duplicate", false);
getApplicationContext().sendBroadcast(addIntent);
}
答案 0 :(得分:0)
您可以在调用方法removeShortcut()
之前使用方法AddShortcutIconToHomeScreen()
删除快捷方式(如果存在):
方式:强>
private void removeShortcut() {
Intent shortcutIntent = new Intent(this, EmployeeListActivity.class);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Intent addIntent = new Intent();
addIntent
.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Device Tracker");
addIntent
.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(addIntent);
}
并在清单文件中添加权限
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
答案 1 :(得分:0)
使用SharedPreference
private void AddShortcutIconToHomeScreen() {
boolean mboolean = false;
SharedPreferences settings = getSharedPreferences("PREFS_NAME", 0);
mboolean = settings.getBoolean("FIRST_RUN", false);
if (!mboolean) {
// do the thing for the first time
settings = getSharedPreferences("PREFS_NAME", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("FIRST_RUN", true);
editor.commit();
Intent shortcutIntent = new Intent(this, EmployeeListActivity.class);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Device Tracker");
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(getApplicationContext(),
R.drawable.ic_launcher));
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
addIntent.putExtra("duplicate", false);
getApplicationContext().sendBroadcast(addIntent);
} else {
// other time your app loads
}
}
添加了AndroidManifest.xml权限。
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />