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类↓
- publicclassWidgetServiceextendsService
- {
-
- @Override
- publicIBinderonBind(Intentintent)
- {
- returnnull;
- }
-
- @Override
- publicvoidonStart(Intentintent,intstartId)
- {
- super.onStart(intent,startId);
- RemoteViewsrv=newRemoteViews(this.getPackageName(),R.layout.app);
- rv.setTextViewText(R.id.av1,newDate().toLocaleString());
- ComponentNamecn=newComponentName(this,DeskMain.class);
- AppWidgetManageram=AppWidgetManager.getInstance(this);
- am.updateAppWidget(cn,rv);
- }
- }
AppWidgetProvider类↓
- publicclassDeskMainextendsAppWidgetProvider
- {
-
- @Override
- publicvoidonUpdate(Contextcontext,AppWidgetManagerappWidgetManager,
- int[]appWidgetIds)
- {
- Log.i("syso","update3...................");
- Intentintent=newIntent(context,WidgetService.class);
- PendingIntentrefreshIntent=PendingIntent.getService(context,0,intent,0);
- AlarmManageralarm=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
- alarm.setRepeating(AlarmManager.RTC,0,1000,refreshIntent);
- context.startService(intent);
- }
-
- @Override
- publicvoidonReceive(Contextcontext,Intentintent)
- {
- Log.i("syso","receive...................");
- super.onReceive(context,intent);
- }
AndroidManifest.xml
- <?xmlversion="1.0"encoding="utf-8"?>
- <manifestxmlns:android="http://schemas.android.com/apk/res/android"
- package="com.app"
- android:versionCode="1"
- android:versionName="1.0">
- <uses-permissionandroid:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
-
-
- <applicationandroid:icon="@drawable/icon"android:label="@string/app_name"android:screenOrientation="portrait"android:permission="android.permission.ACCESS_FINE_LOCATION">
- <activityandroid:name=".Main"
- android:label="@string/app_name">
-
- <receiver
- android:label="@string/app_name"
- android:name=".DeskMain">
- <intent-filter>
- <actionandroid:name="android.appwidget.action.APPWIDGET_UPDATE"/>
- </intent-filter>
- <meta-dataandroid:name="android.appwidget.provider"
- android:resource="@xml/my_widget_provider">
- </meta-data>
- </receiver>
- <serviceandroid:name="WidgetService"/>
-
- </application>
- <uses-sdkandroid:minSdkVersion="7"/>
-
- </manifest>
layout下的app.xml UI布局 xml下的my_widget_provider.xml 提供了UI宽高信息
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"android:orientation="horizontal"android:gravity="center">
- <ImageViewandroid:src="@drawable/icon"android:layout_height="wrap_content"android:layout_width="wrap_content"android:id="@+id/imageView1"></ImageView>
-
- <TextView
- android:gravity="center"android:text="TextView"android:id="@+id/av1"android:layout_width="wrap_content"android:layout_height="wrap_content"></TextView>
- </LinearLayout>
- <?xmlversion="1.0"encoding="utf-8"?>
- <appwidget-providerxmlns:android="http://schemas.android.com/apk/res/android"
- android:minWidth="95dip"
- android:minHeight="73dip"
- android:initialLayout="@layout/app"/>
转自:http://blog.csdn.net/maydie1989/article/details/6460163