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

Java 单击按钮将android应用程序的快捷方式添加到主屏幕,java,android,homescreen,Java,Android,Homescreen,我想通过按下一个按钮使我的应用程序更容易添加到主屏幕。所以我想的是,在我的应用程序底部有一个按钮,上面写着“添加到主屏幕”,当按下它时,它会在不关闭应用程序的情况下添加到主屏幕的快捷方式。 我应该添加什么代码来实现这一点?发送一个包含结果意图的安装快捷方式广播(在本例中,结果意图是直接打开某些活动) 您的清单中还需要此权限: <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" /&

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

发送一个包含结果意图的安装快捷方式广播(在本例中,结果意图是直接打开某些活动)

您的清单中还需要此权限:

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

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

 <!-- 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>
这样做:

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

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

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

根据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.");

我希望这有帮助

对于我们这些仍希望这样做的人,以下是使用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();
    }

所以onclick应该有第三个部分作为结果?是的,你只需要这样做:public void onclick(View v){createShortCut();}在Xiamoi中,它显示一个对话框,该应用程序试图将快捷方式添加到主屏幕,但如果是TrueCaller应用程序,它没有显示原因?谢谢你的解决方案。这对我来说很好。但我还有一个问题。我的应用程序支持多种语言。所以,当我在“设置”中更改手机语言时,我的应用程序名称在launcher中会得到很好的更新。但应用程序名称不会在主屏幕中更新为新语言。我是否需要为此添加更多内容?感谢阅读..因为你不能修改快捷方式,这将是不可能的。我注意到一件奇怪的事情:当使用此代码时,当我按下启动程序上的应用程序图标时,我总是在应用程序的活动中调用“onCreate”方法。即使按下home(主页)按钮,然后从启动器打开应用程序,也会出现这种情况。为什么?@androiddeveloper,这可能是由于为意图设置了标志。我正在看“快捷方式意图”部分。它表示,对于静态快捷方式,
FLAG\u ACTIVITY\u NEW\u TASK
FLAG\u ACTIVITY\u CLEAR\u TASK
将始终被设置,因此“在启动静态快捷方式时,所有现有活动都将被销毁”。如果这是您的场景,那么这就解释了由于强制重新启动活动而产生的额外
onCreate
。在Android O(API 26)中,您可以创建
快捷键fo
,并请求将其固定到主屏幕上。还有一个支持库方法
ShortcutManagerCompat.requestPinShortcut
,它可以在较旧的平台上调用此
com.android.launcher.action.INSTALL_SHORTCUT
广播。不幸的是,文档没有提到清单权限要求,android.support.v4.content.pm.shortcutionfocompat,android.content.IntentSender)确认,旧广播
Intent
方式在Oreo及以上版本上不起作用。开发人员可以在瞄准API26及以上版本时使用(
requestPinShortcut
方法包含回退到
Intent
方法
button.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            createShortCut();
        }
    });
 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.");
    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();
    }