2011-06-27 76 views
11

我想我已经尝试了我在互联网上找到的所有解决方案,但没有人工作 - 没有强制关闭,但桌面上没有任何显示。现在,我已经这样:为桌面上的任何应用程序创建快捷方式

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

我缺少什么?谢谢。

+0

作为参考,编辑你的问题就足以撞起来再次到首页。您可以请提供 –

+1

,提供完整的源代码。 – Nirav

回答

15

已解决。就在明显的改变:

这样的:

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

这样:

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

只是一个 '使用' ¬¬

+0

您的代码似乎产生执行该程序的桌面快捷方式!在运行应用程序之前,我可以在安装应用程序时创建桌面图标。 –

+0

不可以,因为Java代码必须在启动应用程序时运行的主要活动内 –

相关问题