以前对这个东西很感兴趣,因为确实方便,如今有时间了来做一个例子
首先要定义一个layout(widgetview.xml)和一个配置文件(widgetconfig.xml)
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 7 <TextView 8 android:id="@+id/tv" 9 android:layout_width="match_parent" 10 android:layout_height="match_parent" 11 android:text="时间显示" /> 12 13 </LinearLayout>
1 <?xml version="1.0" encoding="utf-8"?> 2 <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" 3 android:initialLayout="@layout/widget" 4 android:minHeight="40dp" 5 android:minWidth="100dp" 6 android:updatePeriodMillis="864000" > 7 8 </appwidget-provider>
然后再实现一个widgetProvider
1 package com.example.widgettest; 2 3 4 import android.appwidget.AppWidgetManager; 5 import android.appwidget.AppWidgetProvider; 6 import android.content.Context; 7 import android.content.Intent; 8 9 public class WidgetProvider extends AppWidgetProvider { 10 @Override 11 public void onDeleted(Context context, int[] appWidgetIds) { 12 super.onDeleted(context, appWidgetIds); 13 // widget被移除 14 } 15 16 @Override 17 public void onDisabled(Context context) { 18 super.onDisabled(context); 19 // 最后一个被移除 20 context.stopService(new Intent(context, TimerService.class)); 21 } 22 23 @Override 24 public void onEnabled(Context context) { 25 super.onEnabled(context); 26 // widget添加到屏幕上 27 context.startService(new Intent(context, TimerService.class)); 28 } 29 30 @Override 31 public void onReceive(Context context, Intent intent) { 32 super.onReceive(context, intent); 33 } 34 35 @Override 36 public void onUpdate(Context context, AppWidgetManager appWidgetManager, 37 int[] appWidgetIds) { 38 super.onUpdate(context, appWidgetManager, appWidgetIds); 39 // 刷新widget 40 } 41 }
通过一个service对provider进行刷新
1 package com.example.widgettest; 2 3 import java.text.SimpleDateFormat; 4 import java.util.Date; 5 import java.util.Timer; 6 import java.util.TimerTask; 7 8 import android.app.Service; 9 import android.appwidget.AppWidgetManager; 10 import android.content.ComponentName; 11 import android.content.Intent; 12 import android.os.IBinder; 13 import android.widget.RemoteViews; 14 15 public class TimerService extends Service { 16 private Timer timer; 17 private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 18 19 @Override 20 public IBinder onBind(Intent intent) { 21 // TODO Auto-generated method stub 22 return null; 23 } 24 25 @Override 26 public void onCreate() { 27 // TODO Auto-generated method stub 28 super.onCreate(); 29 30 timer = new Timer(); 31 timer.schedule(new TimerTask() { 32 33 @Override 34 public void run() { 35 updateViews(); 36 } 37 }, 0, 1000); 38 } 39 40 private void updateViews() { 41 String time = sdf.format(new Date()); 42 RemoteViews rv = new RemoteViews(getPackageName(), R.layout.widget); 43 rv.setTextViewText(R.id.tv, time); 44 AppWidgetManager manager = AppWidgetManager 45 .getInstance(getApplicationContext()); 46 ComponentName cn = new ComponentName(getApplicationContext(), 47 WidgetProvider.class); 48 manager.updateAppWidget(cn, rv); 49 } 50 51 @Override 52 public void onDestroy() { 53 // TODO Auto-generated method stub 54 super.onDestroy(); 55 timer = null; 56 } 57 58 }
最后要注册AndroidManifest文件
1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.example.widgettest" 4 android:versionCode="1" 5 android:versionName="1.0" > 6 7 <uses-sdk 8 android:minSdkVersion="8" 9 android:targetSdkVersion="23" /> 10 11 <application 12 android:allowBackup="true" 13 android:icon="@drawable/ic_launcher" 14 android:label="@string/app_name" > 15 <activity 16 android:name=".MainActivity" 17 android:label="@string/app_name" > 18 <intent-filter> 19 <action android:name="android.intent.action.MAIN" /> 20 21 <category android:name="android.intent.category.LAUNCHER" /> 22 </intent-filter> 23 </activity> 24 25 <service android:name="com.example.widgettest.TimerService" > 26 </service> 27 28 <receiver android:name="com.example.widgettest.WidgetProvider" > 29 <intent-filter> 30 <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> 31 </intent-filter> 32 33 <meta-data 34 android:name="android.appwidget.provider" 35 android:resource="@xml/widgetconfig" /> 36 </receiver> 37 </application> 38 39 </manifest>