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

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

  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

  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. <!---->
  11. <receiver
  12. android:label="@string/app_name"
  13. android:name=".DeskMain">
  14. <intent-filter>
  15. <actionandroid:name="android.appwidget.action.APPWIDGET_UPDATE"/>
  16. </intent-filter>
  17. <meta-dataandroid:name="android.appwidget.provider"
  18. android:resource="@xml/my_widget_provider">
  19. </meta-data>
  20. </receiver>
  21. <serviceandroid:name="WidgetService"/>
  22. </application>
  23. <uses-sdkandroid:minSdkVersion="7"/>
  24. </manifest>

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

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"android:orientation="horizontal"android:gravity="center">
  6. <ImageViewandroid:src="@drawable/icon"android:layout_height="wrap_content"android:layout_width="wrap_content"android:id="@+id/imageView1"></ImageView>
  7. <TextView
  8. android:gravity="center"android:text="TextView"android:id="@+id/av1"android:layout_width="wrap_content"android:layout_height="wrap_content"></TextView>
  9. </LinearLayout>
  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

你可能感兴趣的:(appwidget)