首页 > 代码库 > android清除数据后widget无法更新问题修改

android清除数据后widget无法更新问题修改

清除数据时会发送ACTION_PACKAGE_DATA_CLEARED广播,在widgetProvider里面监听该广播并进行处理就可以了


 public static class PackageDataClearedReceiver extends BroadcastReceiver {
        private static final String ACTION_PACKAGE_DATA_CLEARED = "com.mediatek.intent.action.SETTINGS_PACKAGE_DATA_CLEARED";

        public void onReceive(Context context, Intent intent) {
            if (!ACTION_PACKAGE_DATA_CLEARED.equals(intent.getAction())) {
                return;
            }
            String pkgName = intent.getStringExtra("packageName");
            MusicLogUtils.v(TAG, "PackageDataClearedReceiver recevied pkgName = " + pkgName);
            if (pkgName != null && pkgName.equals(context.getPackageName())) {
                MediaAppWidgetProvider mediaAppWidgetProvider = MediaAppWidgetProvider
                        .getInstance();
                if (mediaAppWidgetProvider == null) {
                    MusicLogUtils.v(TAG, "mediaAppWidgetProvider is null ");
                    return;
                }
                mediaAppWidgetProvider.defaultAppWidget(context, null);
            }
        }

    }

defaultAppWidget()函数会重新加载widget信息和布局文件,这样就可以恢复默认值了。
最后别忘了在AndroidManefest.xml里面加上:

        <receiver android:name="com.android.music.MediaAppWidgetProvider$PackageDataClearedReceiver" >
            <intent-filter>
                <action android:name="com.mediatek.intent.action.SETTINGS_PACKAGE_DATA_CLEARED" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </receiver>


android清除数据后widget无法更新问题修改