开发者

Caching Bitmaps in a Appwidget

I am building an AppWidget which I'm hoping to be skinnable by allowing the user to download skins from a central repository.

I am copying the pngs that make up a skin into the widget's private directory. I was loading the bitmaps from t开发者_运维知识库his directory and making Bitmaps that I stored in a static HashMap in order to avoid loading them each time.

I've realised that this will not work as the HashMap is garbage collected at some point (several hours later typically). I realise it would be possible to load the Bitmaps from disk each time, but this will most likely be slow and drain the battery.

Is there a better way of caching this kind of data?


From looking at the source code for Google's standard "Power control" Widget

https://android.googlesource.com/platform/packages/apps/Settings/+/master/src/com/android/settings/widget/SettingsAppWidgetProvider.java

I figured out that I need to set DONT_KILL_APP in onEnabled

@Override
public void onEnabled(Context context) {
    PackageManager pm = context.getPackageManager();
    pm.setComponentEnabledSetting(
            new ComponentName("com.android.settings", ".widget.SettingsAppWidgetProvider"),
            PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
            PackageManager.DONT_KILL_APP);
}

and unset it again in onDisabled

@Override
public void onDisabled(Context context) {
    PackageManager pm = context.getPackageManager();
    pm.setComponentEnabledSetting(
            new ComponentName("com.android.settings", ".widget.SettingsAppWidgetProvider"),
            PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
            PackageManager.DONT_KILL_APP);
}

This seems to work properly for me, the settings were retained overnight (they weren't previously).

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜