添加快捷键为Android应用程序要主屏幕上按一下按钮 [英] Add Shortcut for android application To home screen On button click

查看:96
本文介绍了添加快捷键为Android应用程序要主屏幕上按一下按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想可以很容易地我的应用程序由pressing一个按钮添加到主屏幕。所以我在想什么是我的应用程序的底部,上面写着一个按钮添加到主屏幕,当它是pressed,它添加快捷方式到主屏幕而不关闭应用程序。 什么code,我应该补充要做到这一点?

I want to make it easy to add my app to home screen by pressing a button. So What I am Thinking is a button at the bottom of my app that says "Add to home screen" and when it is pressed, it adds the shortcut to the home screen without closing the application. what code should I add To do that?

推荐答案

发送INSTALL_SHORTCUT广播用所得意图作为额外的(在这种情况下,结果意图被直接打开某些活性)。

Send an INSTALL_SHORTCUT broadcast with the resulting Intent as an extra (in this case, the result Intent is opening some activity directly).

    //where this is a context (e.g. your current activity)
    final Intent shortcutIntent = new Intent(this, SomeActivity.class);

    final Intent intent = new Intent();
    intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    // Sets the custom shortcut's title
    intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
    // Set the custom shortcut icon
    intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(this, R.drawable.icon));
    // add the shortcut
    intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
    sendBroadcast(intent);

您还需要在你的清单此权限:

You also need this permission in your manifest:

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />

这篇关于添加快捷键为Android应用程序要主屏幕上按一下按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆