如何在Android中以编程方式添加应用程序快捷方式
我需要将我的Android应用程序作为快捷方式添加到主屏幕如何在Android中以编程方式添加应用程序快捷方式,android,Android,我需要将我的Android应用程序作为快捷方式添加到主屏幕 请给出这个想法。如果可能,请告诉我如何管理现有的快捷方式(删除和添加更多的快捷方式)。我读了一篇文章,可以帮助您以编程方式在主屏幕上添加应用程序快捷方式 你可以参考 您也可以参考与快捷方式相关的stackoverflow问题 在第一个屏幕的onCreate()方法中调用此方法。也确保 使用SharedReference(如I)检查应用程序是否首次运行 曾: 我花了相当多的时间尝试stackoverflow的不同解决方案,但大多数都没有用
请给出这个想法。如果可能,请告诉我如何管理现有的快捷方式(删除和添加更多的快捷方式)。我读了一篇文章,可以帮助您以编程方式在主屏幕上添加应用程序快捷方式 你可以参考 您也可以参考与快捷方式相关的stackoverflow问题 在第一个屏幕的onCreate()方法中调用此方法。也确保 使用SharedReference(如I)检查应用程序是否首次运行 曾:
我花了相当多的时间尝试stackoverflow的不同解决方案,但大多数都没有用,因为它们正在启动新的活动实例。我需要一个快捷方式,它的工作原理与应用程序列表中的快捷方式或Google Play自动安装的快捷方式完全相同(启动活动或将已启动的活动放在前面) 别忘了更新您的清单:
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
我认为API不会公开该功能。默认情况下,你会在下拉对话框中看到一个应用程序图标。可能会有其他关于它的链接,例如:和:我们可以通过javascript来做这件事吗?蚂蚁的主意?
@Override
public void onCreate(Bundle savedInstanceState) {
//Save the flag to SharedPreferences to prevent duplicated shortcuts
if (!settings.isShortcutAdded()) {
addShortcut();
settings.setShortcutAdded(true);
}
}
private void addShortcut() {
Intent shortcutIntent = new Intent(getApplicationContext(), MainActivity.class);
shortcutIntent.setAction(Intent.ACTION_MAIN);
shortcutIntent.addCategory(Intent.CATEGORY_LAUNCHER);
int flags = Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED | Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT;
shortcutIntent.addFlags(flags);
Intent addIntent = new Intent();
addIntent.putExtra("duplicate", false);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getResources().getString(R.string.app_name));
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource
.fromContext(getApplicationContext(), R.drawable.ic_launcher));
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(addIntent);
}
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />