问题 为桌面上的任何应用创建快捷方式


我想我已经尝试了我在互联网上找到的所有解决方案,但没有人工作 - 没有强制关闭,但桌面上没有任何内容。现在,我是这样的:

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()是这样的:

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

我错过了什么?谢谢。


7626
2018-06-27 13:23


起源

作为参考,编辑您的问题足以将其再次提升到首页。 - Michael Myers♦
可以吗,提供完整的源代码。 - Nirav


答案:


解决了。只需更改清单:

这个:

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

对此:

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

只是'使用'¬¬


15
2017-07-06 11:53



您的代码似乎在执行程序时生成桌面快捷方式!有没有办法在运行之前在安装应用程序时创建桌面图标。 - Ajith Memana
不,你不能,因为java代码必须在你启动应用程序时运行的主要活动中 - Jad Joubran