设为首页 加入收藏

TOP

Android AppWidgetProvider 定时刷新问题
2014-11-24 11:39:42 来源: 作者: 【 】 浏览:0
Tags:Android AppWidgetProvider 定时 刷新 问题

本来想做一个小时钟


但是在定时刷新的时候遇到了问题


很多资料和书上的方法是


在onUpdate()方法中


用Intent请求Service


Intent intent = new Intent(context, UpdateService.class);
context.startService(intent);
在service中的onStart()或者onReceive()方法中来实现更新界面


而在AndroidManifest.xml中定义一个receiver


android:label="@string/app_name"
android:name=".DeskMain">

android.appwidget.action.APPWIDGET_UPDATE
" />

android:resource="@xml/my_widget_provider" >


DeskMain是你的AppWidgetProvider类 @xml/my_widget_provider是提供UI信息的配置文件 比如宽高


android.appwidget.action.APPWIDGET_UPDATE 书上的解释是 当AppWidget更新时会收到的系统广播信息


但是用eclipse填写xml的时候这个选项已经没有了 于是上网查了一下


在 appwidget-provider 用 android:updatePeriodMillis 这种方式已经在SKD1.5以后被废了



解决的办法找了一下 大概总结了几种


1.在onUpdate()中用自己的Timer和TimerTask解决


2.用Thread


3.Intent intent=new Intent(context ,WidgetService.class);
PendingIntent refreshIntent=PendingIntent.getService(context, 0, intent, 0);
AlarmManager alarm=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC, 0, 1000, refreshIntent);//每秒1次
context.startService(intent);



service类↓



AppWidgetProvider类↓



AndroidManifest.xml



layout下的app.xml UI布局 xml下的my_widget_provider.xml 提供了UI宽高信息


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Android开发之Activity生命周期 下一篇Android OpenGL 画球 画圆

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Linux_百度百科 (2025-12-26 12:51:52)
·Shell 流程控制 | 菜 (2025-12-26 12:51:49)
·TCP/UDP协议_百度百科 (2025-12-26 12:20:11)
·什么是TCP和UDP协议 (2025-12-26 12:20:09)
·TCP和UDP详解 (非常 (2025-12-26 12:20:06)