首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在Android中以编程方式添加应用程序快捷方式

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

Stack Overflow用户
提问于 2010-07-10 11:46:31
回答 2查看 13.9K关注 0票数 12

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

请给出这方面的想法。如果可能,请告诉我如何管理现有的快捷方式(删除和添加更多的快捷方式)。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-07-10 12:43:10

我读过一篇文章,它可以帮助你在主屏幕上以编程方式添加应用程序快捷方式。

您可以参考 example

您还可以参考与快捷方式here 相关的堆栈溢出问题。

票数 9
EN

Stack Overflow用户

发布于 2014-10-17 16:38:20

我花了相当多的时间尝试来自stackoverflow的不同解决方案,但大多数都是无用的,因为它们正在启动新的活动实例。我需要一个快捷键,它的工作方式完全像应用程序列表中的一个或Google Play自动安装的(启动活动或将已经开始的活动带到前面)。

    @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" />
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3217741

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档