widget桌面小控件创建步骤:
1 写一个类extends AppWidgetProvider
2 在清单文件件中注册:
1 <receiver android:name=".ExampleAppWidgetProvider" > 2 <intent-filter> 3 <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> 4 </intent-filter> 5 <meta-data android:name="android.appwidget.provider" 6 android:resource="@xml/example_appwidget_info" /> 7 </receiver>
3 在res/xml创建example_appwidget_info.xml
1 <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" 2 android:minWidth="294dp" 3 android:minHeight="72dp" 4 android:updatePeriodMillis="86400000" 更新时间最少为半个小时,可以不用写,1.6版本以上已经不再使用该属性 5 android:previewImage="@drawable/preview" 预览图片 不用写 一般是应用程序的默认图标 6 android:initialLayout="@layout/example_appwidget" 7 android:configure="com.example.android.ExampleAppWidgetConfigure" 3.0以上的属性,3.0以下可以不使用 8 android:resizeMode="horizontal|vertical"> 默认方向是水平 可以不用写该属性 9 </appwidget-provider>
4 指定布局 example_appwidget.xml
生命周期:
1 添加到桌面:
onEnabled() --> onUpdate()
2 删除
onDeleted()----> onDisabled()
如果桌面已经有一个了widget的实例存在,再次添加onUpdate()
删除之后,如果桌面上还有widget的实例存在,只会调用onDeleted().
更成时间的显示:
1 使用一个Servic来执行时间的更新
Timer TimerTask
示例代码 清单文件代码:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.android.hzy.widget" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="16" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.android.hzy.widget.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name=".ExampleAppWidgetProvider" > <intent-filter> <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> </intent-filter> <meta-data android:name="android.appwidget.provider" android:resource="@xml/example_appwidget_info" /> </receiver> <service android:name=".MyService"></service> </application> </manifest>
2.res/xml下的widget的描述信息
1 <?xml version="1.0" encoding="utf-8"?> 2 <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" 3 android:minWidth="294dp" 4 android:minHeight="72dp" 5 android:initialLayout="@layout/example_appwidget" 6 > 7 8 9 </appwidget-provider>
3.widget布局代码
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 android:background="@android:color/white"> 7 8 <TextView 9 android:id="@+id/tv_time" 10 android:layout_width="match_parent" 11 android:layout_height="match_parent" 12 android:text="我是widget" 13 android:textColor="#f00" 14 android:textSize="20sp" 15 /> 16 17 </LinearLayout>
4.MainActivity代码:
1 package com.android.hzy.widget; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 6 public class MainActivity extends Activity { 7 8 @Override 9 protected void onCreate(Bundle savedInstanceState) { 10 super.onCreate(savedInstanceState); 11 setContentView(R.layout.activity_main); 12 } 13 14 }
5.继承AppWidgetProvider
1 package com.android.hzy.widget; 2 3 import java.text.SimpleDateFormat; 4 5 import android.app.PendingIntent; 6 import android.appwidget.AppWidgetManager; 7 import android.appwidget.AppWidgetProvider; 8 import android.content.Context; 9 import android.content.Intent; 10 import android.util.Log; 11 import android.widget.RemoteViews; 12 13 public class ExampleAppWidgetProvider extends AppWidgetProvider { 14 15 /** 16 * 可用 17 */ 18 @Override 19 public void onEnabled(Context context) { 20 // TODO Auto-generated method stub 21 super.onEnabled(context); 22 Log.i("i", " onEnabled "); 23 24 Intent intent = new Intent(context,MyService.class); 25 context.startService(intent); 26 } 27 28 /** 29 * 更新 30 */ 31 @Override 32 public void onUpdate(Context context, AppWidgetManager appWidgetManager, 33 int[] appWidgetIds) { 34 // TODO Auto-generated method stub 35 super.onUpdate(context, appWidgetManager, appWidgetIds); 36 Log.i("i", " onUpdate "); 37 38 /* RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.example_appwidget); 39 long date = System.currentTimeMillis(); 40 SimpleDateFormat format = new SimpleDateFormat("yyyy--MM--dd hh:mm:ss"); 41 String text = format.format(date); 42 43 views.setTextViewText(R.id.tv_time, text); 44 45 Intent intent = new Intent(context,MainActivity.class); 46 PendingIntent pendingIntent = PendingIntent.getActivity(context, 100, intent, 0); 47 views.setOnClickPendingIntent(R.id.tv_time, pendingIntent); 48 // 更新widget 49 appWidgetManager.updateAppWidget(appWidgetIds, views);*/ 50 } 51 52 /** 53 * 删除 54 */ 55 @Override 56 public void onDeleted(Context context, int[] appWidgetIds) { 57 // TODO Auto-generated method stub 58 super.onDeleted(context, appWidgetIds); 59 Log.i("i", " onDeleted "); 60 } 61 62 /** 63 * 不可用 64 */ 65 @Override 66 public void onDisabled(Context context) { 67 // TODO Auto-generated method stub 68 super.onDisabled(context); 69 Log.i("i", " onDisabled "); 70 71 // 停止服务 72 Intent intent = new Intent(context,MyService.class); 73 context.stopService(intent); 74 } 75 }
6.服务代码:
1 package com.android.hzy.widget; 2 3 import java.text.SimpleDateFormat; 4 import java.util.Timer; 5 import java.util.TimerTask; 6 7 import android.app.PendingIntent; 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 MyService extends Service { 16 17 private Timer timer; 18 private TimerTask task = new TimerTask(){ 19 20 @Override 21 public void run() { 22 // TODO Auto-generated method stub 23 AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getApplicationContext()); 24 25 RemoteViews views = new RemoteViews(getPackageName(), R.layout.example_appwidget); 26 long date = System.currentTimeMillis(); 27 SimpleDateFormat format = new SimpleDateFormat("yyyy--MM--dd hh:mm:ss"); 28 String text = format.format(date); 29 30 views.setTextViewText(R.id.tv_time, text); 31 32 Intent intent = new Intent(getApplicationContext(),MainActivity.class); 33 PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 100, intent, 0); 34 views.setOnClickPendingIntent(R.id.tv_time, pendingIntent); 35 36 ComponentName provider = new ComponentName(getApplicationContext(), ExampleAppWidgetProvider.class); 37 // 更新appWidget 38 appWidgetManager.updateAppWidget(provider, views); 39 } 40 41 }; 42 43 @Override 44 public void onCreate() { 45 // TODO Auto-generated method stub 46 super.onCreate(); 47 48 // 开启线程 49 timer = new Timer(); 50 timer.schedule(task, 1000, 1000); 51 52 } 53 54 55 @Override 56 public IBinder onBind(Intent arg0) { 57 // TODO Auto-generated method stub 58 return null; 59 } 60 61 @Override 62 public void onDestroy() { 63 // TODO Auto-generated method stub 64 super.onDestroy(); 65 timer.cancel(); 66 task = null; 67 } 68 69 }