开发者

Android AppWidgetProviderInfo icon id

Android's AppWidgetProviderInfo class contains an integer icon field. I want to get hold of the icon as a Drawable.

I presume the icon id needs to be resolved against some package's Resource开发者_如何学运维s. The widget provider is not part of my app, so my own app resources as returned by getResources() are no use. Neither are the system resources given by Resources.getSystem().

How can I convert this integer id into a Drawable?


Use PackageManager and getResourcesForApplication().


Like CommonsWare has said you will need to use getResourcesForApplication() here is how you can use it and also since lollipop there is a loadIcon() function that's a lot easier to use

Drawable icon = null;
if (Build.VERSION.SDK_INT >= 21) {
    icon = info.loadIcon(this, density);
} else {
    try {
        String packageName = info.provider.getPackageName();
        Resources resources = context.getPackageManager().getResourcesForApplication(packageName);
        icon = resources.getDrawable(info.icon);
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜