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>??
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。