上班天天打酱油 ,哎无事 就学习了下android Widgets 桌面小布件,先看下文档
本人英文不好 直接上代码了
先写个类继承AppWidgetProvider
package com.test.App_widget; import java.util.Calendar; import android.app.PendingIntent; import android.appwidget.AppWidgetManager; import android.appwidget.AppWidgetProvider; import android.content.Context; import android.content.Intent; import android.util.Log; import android.widget.RemoteViews; public class MyApp_widget extends AppWidgetProvider { private Context context; @Override public void onUpdate( Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { updateAppWidget(context, appWidgetManager, appWidgetIds); context.startService(new Intent(context,MyServices.class)); } static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int [] appWidgetIds) { Log.e("updateAppWidget", "updateAppWidget---------------------onStart"); int N = appWidgetIds.length; for (int i = 0; i < N; i++) { int appWidgetId = appWidgetIds[i]; // Create an Intent to launch ExampleActivity //Intent intent = new Intent(context, MainActivity.class); //PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, // intent, 0); // Get the layout for the App Widget and attach an on-click listener // to the button RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.main); Calendar calendar=Calendar.getInstance(); int hour=calendar.get(Calendar.HOUR); int minute=calendar.get(Calendar.MINUTE); int second=calendar.get(Calendar.SECOND); if(minute<10){ views.setTextViewText(R.id.textView1, ""+hour+":0"+minute+":"+second); }else if(second<10){ views.setTextViewText(R.id.textView1, ""+hour+":"+minute+":0"+second); }else if(minute<10&&second<10){ views.setTextViewText(R.id.textView1, ""+hour+":0"+minute+":"+second); } else{ views.setTextViewText(R.id.textView1, ""+hour+":"+minute+":"+second); } // views.setOnClickPendingIntent(R.id.button, pendingIntent); // Tell the AppWidgetManager to perform an update on the current App // Widget appWidgetManager.updateAppWidget(appWidgetId, views); } } }
当你在添加小部件时 ,他会自己调用onUpdate方法
然后是配置
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.test.App_widget" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="7" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <receiver android:name=".MyApp_widget"> <intent-filter> <action android:name="android.appwidget.action.APPWIDGET_UPDATE"></action> </intent-filter> <meta-data android:name="android.appwidget.provider" android:resource="@xml/appwidget_info" /> </receiver> <service android:name=".MyServices"></service> </application> </manifest>
其实这个类也是这个广播接收器 ,重要的是
<meta-data android:name="android.appwidget.provider" android:resource="@xml/appwidget_info" />
我们来看下这个文件
<?xml version="1.0" encoding="utf-8"?> <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:minWidth="294dp" android:minHeight="72dp" android:updatePeriodMillis="86400000" android:initialLayout="@layout/main" > </appwidget-provider>
这些属性大家也知道是什么意思 ,一定是指定这个小部件的大小样式等
下面看下 怎么更新这个小部件的样式 ,比如是 个时钟 第一秒是不是要刷新一次界面,这里 我们写个services
package com.test.App_widget; import android.app.Service; import android.appwidget.AppWidgetManager; import android.content.ComponentName; import android.content.Intent; import android.os.Handler; import android.os.IBinder; import android.util.Log; public class MyServices extends Service{ @Override public IBinder onBind(Intent arg0) { return null; } @Override public void onStart(final Intent intent, final int startId) { Log.e("MyServices", "MyServices---------------------onStart"); super.onStart(intent, startId); AppWidgetManager appWidgetManager=AppWidgetManager.getInstance(this); int []appWidgetIds =appWidgetManager.getAppWidgetIds(new ComponentName(this, MyApp_widget.class)); MyApp_widget.updateAppWidget(this, appWidgetManager, appWidgetIds); new Handler().postDelayed(new Runnable() { @Override public void run() { onStart(intent, startId); } }, 1000); } }
这样 就可以