android AppWidgetProvider 定时刷新问题

AppWidgetProvider是专门用来制作android桌面组件的东东

本来想做一个小时钟

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

很多资料和书上的方法是

在onUpdate()方法中

用Intent请求Service

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

而在AndroidManifest.xml中定义一个receiver

    <receiver
      android:label="@string/app_name"
      android:name=".DeskMain">
            <intent-filter> 
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> 
            </intent-filter> 
     <meta-data android:name="android.appwidget.provider"
        android:resource="@xml/my_widget_provider" >
      </meta-data>
    </receiver>
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解决

http://www.eoeandroid.com/thread-50955-1-1.html

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宽高信息

 

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值