2010-07-21 41 views
6

下面是我的代碼來創建一個選定應用程序的快捷方式。我真的沒有問題,應用程序工作得很好。創建快捷方式:我如何使用可繪製的Icon作爲工具?

的問題是,我能夠創建一個快捷方式從我的應用程序的ressource:

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

但我真的會與自定義繪製喜歡。 (Drawable myDrawable = .....)

我該怎麼辦?

ResolveInfo launchable=adapter.getItem(position); 
    final Intent shortcutIntent = new Intent(); 
    ActivityInfo activity=launchable.activityInfo; 
    ComponentName name=new ComponentName(activity.applicationInfo.packageName,activity.name);  
    shortcutIntent.setComponent(name); 
    shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    final Intent intent = new Intent(); 
    intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); 
    // Sets the custom shortcut's title 
    intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, launchable.loadLabel(pm)); 
    // Set the custom shortcut icon 
    intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(this, R.drawable.icon)); 

    // add the shortcut 
    intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT"); 
    sendBroadcast(intent); 
    finish(); 

感謝很多關於任何線索

+0

ShortcutIconResource.fromContext(pkgContext,iconId)做TRIC ķ! http://stackoverflow.com/questions/17339231/create-shortcut-for-thrid-party-app-is-that-possible/17561676#17561676 – thecr0w 2013-07-10 02:50:10

回答

26

終於找到了解決辦法;我是愚蠢的使用Intent.EXTRA_SHORTCUT_ICON_RESOURCE:

下面是正確的代碼:

Drawable iconDrawable = (....); 
BitmapDrawable bd = (BitmapDrawable) iconDrawable; 
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, bd.getBitmap()); 
3

我無法評論,因爲我只有23聲譽,但我用你的代碼,這是有用的。 但我的形象並沒有快捷方式正確縮放,讓我發現了兩個有趣的話題,可以完善解決方案:

要得到一個快捷方式圖標的正確大小: https://stackoverflow.com/a/19003905/3741926

要擴展您的繪製(使用你以前的函數計算的大小) https://stackoverflow.com/a/10703256/3741926

與所有我得到一個正確縮放圖標快捷方式,希望這將有助於

相關問題