将安卓应用程序的快捷方式添加到主屏幕按钮单击



我想通过按按钮轻松地将我的应用程序添加到主屏幕。所以我在想的是我的应用程序底部的一个按钮,上面写着"添加到主屏幕",当按下它时,它会在不关闭应用程序的情况下将快捷方式添加到主屏幕。为此,我应该添加什么代码?

发送一个INSTALL_SHORTCUT广播,并将生成的意图作为额外的意图(在这种情况下,结果意图直接打开一些活动)。

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

好的...我知道这是旧线程,但我想确保访问此线程的工程师拥有最新信息。

从 Android O 开始 - 作为背景检查限制(在本例中为隐式接收器)的一部分,com.android.launcher.action.INSTALL_SHORTCUT广播不再对您的应用产生任何影响,因为它现在是私有的隐式广播。

Per Android O ActivityManagerService.java :

 case "com.android.launcher.action.INSTALL_SHORTCUT":
                // As of O, we no longer support this broadcasts, even for pre-O apps.
                // Apps should now be using ShortcutManager.pinRequestShortcut().
                Log.w(TAG, "Broadcast " + action
                        + " no longer supported. It will not be delivered.");

我希望这有帮助!

第一步,你应该让午餐者可以接收广播:

 <!-- Intent received used to install shortcuts from other applications -->
    <receiver
        android:name="com.android.launcher2.InstallShortcutReceiver"
        android:permission="com.android.launcher.permission.INSTALL_SHORTCUT">
        <intent-filter>
            <action android:name="com.android.launcher.action.INSTALL_SHORTCUT"/>
        </intent-filter>
    </receiver>

接下来,在清单中添加权限.xml

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

最后,创建一个函数并在单击按钮时调用它:

public void createShortCut(){
    // a Intent to create a shortCut
    Intent shortcutintent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
    //repeat to create is forbidden
    shortcutintent.putExtra("duplicate", false);
    //set the name of shortCut
    shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.shortcutname));
    //set icon
    Parcelable icon = Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.icon);
    shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
    //set the application to lunch when you click the icon
    shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(getApplicationContext() , EnterActivity.class));
    //sendBroadcast,done
    sendBroadcast(shortcutintent);
}

这样做:

button.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            createShortCut();
        }
    });

对于我们这些仍然希望这样做的人来说,这里有更新的代码可以与Android O及更高版本一起使用:

    ShortcutManager shortcutManager = (ShortcutManager) getSystemService(Context.SHORTCUT_SERVICE);
    if (shortcutManager.isRequestPinShortcutSupported()) {
        ShortcutInfo shortcutInfo = new ShortcutInfo.Builder(getApplicationContext(), "1")
                .setIntent(new Intent(getApplicationContext(), DestionationAcitivity.class).setAction(Intent.ACTION_MAIN))
                .setShortLabel("My Shortcut")
                .setIcon(Icon.createWithResource(this, R.drawable.yourDrawable))
                .build();
        shortcutManager.requestPinShortcut( shortcutInfo, null);
    } else {
        Toast.makeText(MainActivity.this,"Creating Shortcuts is not Supported on this Launcher",Toast.LENGTH_SHORT).show();
    }

最新更新