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

以上是来客网为你收集整理的java – 添加Android应用程序的快捷方式到主屏幕按钮点击全部内容,希望文章能够帮你解决java – 添加Android应用程序的快捷方式到主屏幕按钮点击所遇到的程序开发问题。

如果觉得来客网网站内容还不错,欢迎将来客网网站推荐给程序员好友。