android - 如何为桌面上的任何应用程序创建快捷方式?

标签 android android-permissions android-shortcut

我想我已经尝试了我在 Internet 上找到的所有解决方案,但没有一个有效 - 没有强制关闭,但桌面上什么也没有出现。

现在,我有这个:

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;
}

AndroidManifest.xml 文件中:

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

我错过了什么?谢谢。

最佳答案

已解决。只需更改 list :

这个:

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

为此:

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

只是一个“用途” ¬¬

关于android - 如何为桌面上的任何应用程序创建快捷方式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6493518/

相关文章:

android - 使用 SAX 解析递归 XML

android - 使用了错误的资源

android - 如何从具有 singleInstance 启动模式的 Activity 中正确启动语音识别 Activity ?

android - 如何在没有 root 的情况下使用 adb 授予权限?

android - 从 android 启动服务时出现权限问题

android - 如何从 Android 应用程序运行 root 命令?

android - 如何删除应用程序快捷方式图标中的徽章?

android - 在 Mac 中更新到 Android Studio 4 后快捷方式不起作用?

android - variant.getOutputFile() 已弃用。改为在variant.getOutputs()之一上调用它