繁体   English   中英

为android应用程序添加快捷方式到主屏幕点击按钮

[英]Add Shortcut for android application To home screen On button click

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

发送一个 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" />

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

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

每个 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>

接下来,在 manifest.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();
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM