“窗口小部件”可以是一个单独的apk,当然也可以内嵌在其他的apk当中。
此处主要是为了说明“窗口小部件”的实现方法,所有单独做成一个apk 。新建一个android项目(没有activity)。
下面是代码:
首先是Contants类:
package com.android.flashlight; import android.util.Log; public class Contants { private static final String TAG = "flash"; public static final boolean DEBUG = true; public static final String ACTION_FLASHLIGHT_WIGET_CLICK = "com.lenovo.flashlight.action.WIGET_CLICK"; public static void LogE(String msg){ if(DEBUG){ Log.e(TAG, msg); } } public static void LogD(String msg){ if(DEBUG){ Log.d(TAG, msg); } } public static void LogI(String msg){ if(DEBUG){ Log.i(TAG, msg); } } }
在res/layout/下创建文件flashlight_widget_layout.xml布局文件,是“窗口小部件”在桌面上显示的布局文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <!-- 这里是窗口小部件的图标 --> <ImageView android:id="@+id/imageview_widget" android:layout_width="fill_parent" android:layout_height="wrap_content" android:src="@drawable/flashlight_icon" /> </LinearLayout>
package com.android.flashlight; import android.app.PendingIntent; import android.appwidget.AppWidgetManager; import android.appwidget.AppWidgetProvider; import android.content.Context; import android.content.Intent; import android.widget.RemoteViews; public class FlashSwitchWidgetProvider extends AppWidgetProvider { private static RemoteViews mRemoteViews; /** * 每删除一次窗口小部件就调用一次 */ @Override public void onDeleted(Context context, int[] appWidgetIds) { super.onDeleted(context, appWidgetIds); Contants.LogI("onDeleted"); } /** * 当最后一个该窗口小部件删除时调用该方法,注意是最后一个 */ @Override public void onDisabled(Context context) { super.onDisabled(context); Contants.LogI("onDisabled"); } /** * 当该窗口小部件第一次添加到桌面时调用该方法,可添加多次但只第一次调用 */ @Override public void onEnabled(Context context) { super.onEnabled(context); Contants.LogI("onEnabled"); } /** * 接收窗口小部件点击时发送的广播 */ @Override public void onReceive(Context context, Intent intent) { super.onReceive(context, intent); Contants.LogI("onReceive : action = " + intent.getAction()); if(Contants.ACTION_FLASHLIGHT_WIGET_CLICK.equals(intent.getAction())){ //这里是点击“窗口小部件”要执行的内容 } } /** * 每次窗口小部件被点击更新都调用一次该方法 */ @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { super.onUpdate(context, appWidgetManager, appWidgetIds); Contants.LogI("onUpdate"); final int counter = appWidgetIds.length; Contants.LogD("counter = " + counter); for (int i = 0; i < counter; i++) { int appWidgetId = appWidgetIds[i]; onWidgetUpdate(context, appWidgetManager, appWidgetId); } } /** * 窗口小部件更新 * @param context * @param appWidgeManger * @param appWidgetId */ private void onWidgetUpdate(Context context, AppWidgetManager appWidgeManger, int appWidgetId) { Contants.LogD("appWidgetId = " + appWidgetId); mRemoteViews = new RemoteViews(context.getPackageName(), R.layout.flashlight_widget_layout); //"窗口小部件"点击事件发送的Intent广播 Intent intentClick = new Intent(); intentClick.setAction(Contants.ACTION_FLASHLIGHT_WIGET_CLICK); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intentClick, 0); mRemoteViews.setOnClickPendingIntent(R.id.imageview_widget, pendingIntent); appWidgeManger.updateAppWidget(appWidgetId, mRemoteViews); } }
说明见注释内容。
之后是清单文件AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.android.flashlight" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="14" /> <application android:label="@string/app_name" > <receiver android:name=".FlashSwitchWidgetProvider" android:icon="@drawable/flashlight_icon"> <meta-data android:name="android.appwidget.provider" android:resource="@xml/widget_info"></meta-data> <intent-filter> <action android:name="com.lenovo.flashlight.action.WIGET_CLICK"/> <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> </intent-filter> </receiver> </application> </manifest>
其中,res/xml/widget_info.xml:
<?xml version="1.0" encoding="utf-8"?> <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:minWidth="100dp" android:minHeight="40dp" android:updatePeriodMillis="86400000" android:initialLayout="@layout/flashlight_widget_layout" > </appwidget-provider>
这里有个Demo:http://download.csdn.net/detail/liu_zhen_wei/4801779
包括widget窗口小部件的用法和手电筒的功能实现,安装后,添加窗口小部件(手电筒)到桌面,然后点击小部件后,打开手电筒的界面
点击界面,实现 打开/关闭 手电筒。