首页 > 移动平台 > 详细

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

时间:2014-11-22 02:40:38      阅读:326      评论:0      收藏:0      [点我收藏+]

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

?

?

[java]?view plaincopybubuko.com,布布扣bubuko.com,布布扣
?
  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 plaincopybubuko.com,布布扣bubuko.com,布布扣
?
  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>??

bubuko.com,布布扣

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

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

原文:http://jlins.iteye.com/blog/2159022

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!