2010-07-21 15 views
6

Vous trouverez ci-dessous Mon code pour créer un raccourci vers une application sélectionnée. Je n'ai vraiment aucun problème et l'application fonctionne très bien.Création d'un raccourci: comment puis-je travailler avec un dessinable en tant qu'icône?

Le problème est que je suis en mesure de créer un raccourci avec un de ma demande ressource:

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

Mais je voudrais vraiment avec un drawable personnalisé. (Drawable myDrawable = .....)

Comment puis-je faire?

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

Merci beaucoup pour tout indice

+0

ShortcutIconResource.fromContext (pkgContext, iconID) faire le tric k! http://stackoverflow.com/questions/17339231/create-shortcut-for-thrid-party-app-is-that-possible/17561676#17561676 – thecr0w

Répondre

26

a finalement trouvé une solution; J'étais stupide d'utiliser Intent.EXTRA_SHORTCUT_ICON_RESOURCE:

Voici le code correct:

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

Je ne peux pas commenter puisque j'ai seulement 23 réputation, mais je votre code et il était utile. Mais mon image ne pas correctement mis à l'échelle sur le raccourci, donc j'ai trouvé deux sujets intéressants qui peuvent compléter la solution:

Pour obtenir la taille correcte pour une icône de raccourci: https://stackoverflow.com/a/19003905/3741926

à l'échelle de votre drawable (en utilisant la taille que vous avez calculé avec la fonction précédente) https://stackoverflow.com/a/10703256/3741926

avec tout ce que je suis un raccourci avec une icône à l'échelle correctement, espérons qu'il aidera