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

Cre*_*ter 10 java android homescreen

我想通过按下按钮将我的应用程序添加到主屏幕变得容易.所以我在想的是我的应用程序底部的一个按钮,显示"添加到主屏幕",当按下它时,它会将快捷方式添加到主屏幕而不关闭应用程序.我应该添加什么代码?

rof*_*son 25

使用生成的Intent作为额外发送INSTALL_SHORTCUT广播(在这种情况下,结果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);
Run Code Online (Sandbox Code Playgroud)

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

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
Run Code Online (Sandbox Code Playgroud)


小智 7

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

从 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.");
Run Code Online (Sandbox Code Playgroud)

我希望这有帮助 !


Lao*_*izi 5

第一步,你应该让luncher接收广播:

 <!-- 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>
Run Code Online (Sandbox Code Playgroud)

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

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
Run Code Online (Sandbox Code Playgroud)

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

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);
}
Run Code Online (Sandbox Code Playgroud)

像这样做:

button.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            createShortCut();
        }
    });
Run Code Online (Sandbox Code Playgroud)