|
本来想做一个小时钟
但是在定时刷新的时候遇到了问题
很多资料和书上的方法是
在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宽高信息
|