创建一个快捷方式:我怎么可以用绘制为图标的工作? [英] Creating a shortcut: how can I work with a drawable as Icon?
本文介绍了创建一个快捷方式:我怎么可以用绘制为图标的工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面是我的code创建到选定的应用程序的快捷方式。我真的没有任何问题与应用工作得很好。
Below is My code to create a shortcut to a selected application. I have really no problem and the application work quite well.
现在的问题是,我能够创建一个快捷方式从我的应用程序的ressource:
The problem is that I am able to create a shortcut with a ressource from my application:
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(this, R.drawable.icon));
不过,我真的想用一个自定义绘制。 (可绘制myDrawable = .....)
But I really would like with a custom drawable. ( 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();
感谢很多的任何线索
推荐答案
终于找到了解决办法;我是愚蠢的使用Intent.EXTRA_SHORTCUT_ICON_RESOURCE:
Finally found a solution; I was stupid to use Intent.EXTRA_SHORTCUT_ICON_RESOURCE:
下面是正确的code:
Here is the correct code:
Drawable iconDrawable = (....);
BitmapDrawable bd = (BitmapDrawable) iconDrawable;
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, bd.getBitmap());
这篇关于创建一个快捷方式:我怎么可以用绘制为图标的工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文