2013-06-26 118 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

您必須將代碼放入適當的菜單處理函數或按鈕監聽器中 –

相關問題