微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java – 添加Android应用程序的快捷方式到主屏幕按钮点击

我想通过按一下按钮轻松地将我的应用程序添加到主屏幕.所以我在想什么是我的应用程序底部一个按钮,它表示“添加到主屏幕”,按下它后,它将快捷方式添加到主屏幕,而不关闭应用程序.
我应该添加什么代码呢?

解决方法

发送INSTALL_SHORTCUT广播与产生的Intent作为额外的(在这种情况下,结果Intent直接打开一些活动).
//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);

您的清单还需要此权限:

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

原文地址:https://www.jb51.cc/android/125813.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐