android AppWidgetProvider 定时刷新问题

阅读更多

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

本来想做一个小时钟

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

很多资料和书上的方法是

在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解决

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类↓

[java] view plain copy print ?
  1. publicclassWidgetServiceextendsService
  2. {
  3. @Override
  4. publicIBinderonBind(Intentintent)
  5. {
  6. returnnull;
  7. }
  8. @Override
  9. publicvoidonStart(Intentintent,intstartId)
  10. {
  11. super.onStart(intent,startId);
  12. RemoteViewsrv=newRemoteViews(this.getPackageName(),R.layout.app);
  13. rv.setTextViewText(R.id.av1,newDate().toLocaleString());
  14. ComponentNamecn=newComponentName(this,DeskMain.class);
  15. AppWidgetManageram=AppWidgetManager.getInstance(this);
  16. am.updateAppWidget(cn,rv);
  17. }
  18. }

AppWidgetProvider类↓

[java] view plain copy print ?
  1. publicclassDeskMainextendsAppWidgetProvider
  2. {
  3. @Override
  4. publicvoidonUpdate(Contextcontext,AppWidgetManagerappWidgetManager,
  5. int[]appWidgetIds)
  6. {
  7. Log.i("syso","update3...................");
  8. Intentintent=newIntent(context,WidgetService.class);
  9. PendingIntentrefreshIntent=PendingIntent.getService(context,0,intent,0);
  10. AlarmManageralarm=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
  11. alarm.setRepeating(AlarmManager.RTC,0,1000,refreshIntent);
  12. context.startService(intent);
  13. }
  14. @Override
  15. publicvoidonReceive(Contextcontext,Intentintent)
  16. {
  17. Log.i("syso","receive...................");
  18. super.onReceive(context,intent);
  19. }

AndroidManifest.xml

[xhtml] view plain copy print ?
  1. xmlversion="1.0"encoding="utf-8"?>
  2. <manifestxmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.app"
  4. android:versionCode="1"
  5. android:versionName="1.0">
  6. <uses-permissionandroid:name="android.permission.ACCESS_FINE_LOCATION">uses-permission>
  7. <applicationandroid:icon="@drawable/icon"android:label="@string/app_name"android:screenOrientation="portrait"android:permission="android.permission.ACCESS_FINE_LOCATION">
  8. <activityandroid:name=".Main"
  9. android:label="@string/app_name">
  10. <receiver
  11. android:label="@string/app_name"
  12. android:name=".DeskMain">
  13. <intent-filter>
  14. <actionandroid:name="android.appwidget.action.APPWIDGET_UPDATE"/>
  15. intent-filter>
  16. <meta-dataandroid:name="android.appwidget.provider"
  17. android:resource="@xml/my_widget_provider">
  18. meta-data>
  19. receiver>
  20. <serviceandroid:name="WidgetService"/>
  21. application>
  22. <uses-sdkandroid:minSdkVersion="7"/>
  23. manifest>

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

[java] view plain copy print ?
  1. "1.0"encoding="utf-8"?>
  2. xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"android:orientation="horizontal"android:gravity="center">
  5. "@drawable/icon"android:layout_height="wrap_content"android:layout_width="wrap_content"android:id="@+id/imageView1">
  6. android:gravity="center"android:text="TextView"android:id="@+id/av1"android:layout_width="wrap_content"android:layout_height="wrap_content">
[xhtml] view plain copy print ?
  1. xmlversion="1.0"encoding="utf-8"?>
  2. <appwidget-providerxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:minWidth="95dip"
  4. android:minHeight="73dip"
  5. android:initialLayout="@layout/app"/>

转自:http://blog.csdn.net/maydie1989/article/details/6460163

你可能感兴趣的:(android AppWidgetProvider 定时刷新问题)