2013-06-26 43 views
0

This person was generous enough to add his code to his question。但我不能让从何处获取下列变量:向主屏幕添加android启动器快捷方式

resapp_idp

BitmapDrawable bd = (BitmapDrawable) (res.get(app_id).activityInfo.loadIcon(p).getCurrent()); 

这里是他的代码:

Intent shortcutIntent = new Intent(); 
shortcutIntent.setClassName(ai.packageName, ai.name); 
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
shortcutIntent.addCategory(Intent.ACTION_PICK_ACTIVITY); 
Intent intent = new Intent(); 
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); 
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, appName); 

BitmapDrawable bd=(BitmapDrawable)(res.get(app_id).activityInfo.loadIcon(p).getCurrent()); 
Bitmap newbit; 
newbit=bd.getBitmap(); 
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, newbit); 

intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT"); 
context.sendBroadcast(intent); 

此外,是否有任何我必须添加到清单文件?

编辑:

intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, 
     Intent.ShortcutIconResource.fromContext(this, R.drawable.launcher_icon)); 

更换

BitmapDrawable bd=(BitmapDrawable)(res.get(app_id).activityInfo.loadIcon(p).getCurrent()); 
Bitmap newbit; 
newbit=bd.getBitmap(); 
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, newbit); 

,它仍然无法正常工作。这就是为什么我想知道清单文件。

+0

阅读文档调用的函数。这里描述了这些参数的含义。 –

+0

看来你没有努力去自己理解和修改代码 –

+0

@MichaelButscher你知道我的新代码不工作么?清单中是否有要添加的内容? –

回答

1

在清单中,你需要

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

每次启动应用程序时,它都会创建一个附加的快捷方式。有没有办法指定只创建一个快捷方式? –

+0

您必须将代码放入适当的菜单处理函数或按钮监听器中 –

相关问题