How do I create a shortcut for any app on desktop?

隐身守侯 提交于 2019-12-03 07:01:39

问题


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 have 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();

}

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

And in the AndroidManifest.xml file:

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

What am I missing? Thanks.


回答1:


Solved. Just change at manifest:

this:

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

to this:

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

Just an 'uses' ¬¬



来源:https://stackoverflow.com/questions/6493518/how-do-i-create-a-shortcut-for-any-app-on-desktop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!