如何在Android中通过编程添加应用程序快捷方式

12

我需要以编程方式将我的Android应用程序添加到主屏幕上作为快捷方式。

请提供实现此功能的思路。如果可能,请告诉我如何管理现有的快捷方式(删除和添加一些更多的快捷方式)。


我认为API没有暴露那个功能。默认情况下,您会在拉起对话框中获得应用程序图标。 - user132014
3个回答

9

我读了一篇文章,可以帮助您程序化地在主屏幕上添加应用程序快捷方式。

您可以参考此示例

您还可以参考与快捷方式相关的stackoverflow问题这里


可能还有其他相关链接,比如这个:http://code.google.com/p/apps-for-android/source/browse/#git%2FAnyCut 和这个:https://groups.google.com/forum/?fromgroups=#!topic/android-developers/B9n6PjtTKic - android developer

5
在你的第一个屏幕的onCreate()方法中调用此方法。同时,确保使用像我一样的SharedPreferences检查应用程序是否是第一次运行:
 private void addShortcut() {
    //Adding shortcut for MainActivity on Home screen
    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, this.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);
}

    // TO check app is installed first time.
    SharedPreferences prefs = getSharedPreferences("ShortCutPrefs", MODE_PRIVATE);
    if(!prefs.getBoolean("isFirstTime", false)){
        addShortcut();
        SharedPreferences.Editor editor = prefs.edit();
        editor.putBoolean("isFirstTime", true);
        editor.commit();
    } 

似乎不适用于最新的Android版本。 - user924

4

我花了很多时间尝试从stackoverflow上寻找不同的解决方案,但大部分都无用,因为它们会启动新的Activity实例。我需要一种像应用程序列表中那样工作的快捷方式,或者像Google Play自动安装的那个一样(启动Activity或将已经启动的Activity置于前台)。

    @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" />

我们能通过JavaScript完成这件事吗?有什么想法吗? - Curious_k.shree

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接