对桌面的任何应用程序创建一个快捷方式创建一个、快捷方式、应用程序、桌面

2023-09-05 06:54:08 作者:陌上花開緩緩歸 *

我想我已经尝试了所有我在互联网上找到了解决方案,但没有一个人 - 没有工作紧密力,但没有出现在桌面 - 。现在,我已经这样的:

I think I've tried all the solutions I found on the internet, but no one worked -no force close, but nothing appears on desktop-. Now, I've this:

private void createShortcutOnDesktop(Application app) {

    Intent shortcutIntent = new Intent();
    shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, app.getIntentShortcut());
    shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, app.getName());
    shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(context, R.drawable.home_button));
    shortcutIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
    this.sendBroadcast(shortcutIntent);
    finish();

}

在app.getIntentShortcut()是这样的:

The app.getIntentShortcut() is this:

public Intent getIntentShortcut()
{       

    Intent i = new Intent();
    i.setClassName(packageName, name);
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    return i;
}

和在清单:

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

我在想什么?谢谢你。

What am I missing? Thanks.

推荐答案

解决。只要改变在清单:

Solved. Just change at manifest:

这样的:

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

这样:

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

只是一个'使用'¬¬

Just an 'uses' ¬¬