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

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

?

?

[java]?view plaincopy
?
  1. public?static?class?PackageDataClearedReceiver?extends?BroadcastReceiver?{??
  2. ???????private?static?final?String?ACTION_PACKAGE_DATA_CLEARED?=?"com.mediatek.intent.action.SETTINGS_PACKAGE_DATA_CLEARED";??
  3. ??
  4. ???????public?void?onReceive(Context?context,?Intent?intent)?{??
  5. ???????????if?(!ACTION_PACKAGE_DATA_CLEARED.equals(intent.getAction()))?{??
  6. ???????????????return;??
  7. ???????????}??
  8. ???????????String?pkgName?=?intent.getStringExtra("packageName");??
  9. ???????????MusicLogUtils.v(TAG,?"PackageDataClearedReceiver?recevied?pkgName?=?"?+?pkgName);??
  10. ???????????if?(pkgName?!=?null?&&?pkgName.equals(context.getPackageName()))?{??
  11. ???????????????MediaAppWidgetProvider?mediaAppWidgetProvider?=?MediaAppWidgetProvider??
  12. ???????????????????????.getInstance();??
  13. ???????????????if?(mediaAppWidgetProvider?==?null)?{??
  14. ???????????????????MusicLogUtils.v(TAG,?"mediaAppWidgetProvider?is?null?");??
  15. ???????????????????return;??
  16. ???????????????}??
  17. ???????????????mediaAppWidgetProvider.defaultAppWidget(context,?null);??
  18. ???????????}??
  19. ???????}??
  20. ??
  21. ???}??

?

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

?

[html]?view plaincopy
?
  1. <receiver?android:name="com.android.music.MediaAppWidgetProvider$PackageDataClearedReceiver"?>??
  2. ????<intent-filter>??
  3. ????????<action?android:name="com.mediatek.intent.action.SETTINGS_PACKAGE_DATA_CLEARED"?/>??
  4. ????????<category?android:name="android.intent.category.DEFAULT"?/>??
  5. ????</intent-filter>??
  6. </receiver>??

欢迎大家访问我的个人网站 萌萌的IT人

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