AppWidget简单理解就是应用程序的手机桌面快捷方式,也就是说点快捷方式和点菜单里的图标效果是一样的,通过这种机制可以打开应用程序。
AppWidget是BroadCast的一个子类,因此这个类和广播的操作是类似的,下面就一步一步来弄下这个吧
1.编写该组件对于桌面图标的快捷方式,一般来说就是一个图标
<?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:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/mldn_3g" android:id="@+id/img" /> </LinearLayout>
<?xml version="1.0" encoding="UTF-8"?> <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:minHeight="80px" android:minWidth="300px" android:updatePeriodMillis="6000" android:initialLayout="@layout/appwidget"> </appwidget-provider>
<receiver android:name=".MyAppwidget"> <intent-filter> <action android:name="android.appwidget.action.APPWIDGET_UPDATE"></action> </intent-filter> <!-- <intent-filter> <action android:name="org.lxh.action.MYAPPWIDGET_UPDATE" /> </intent-filter> --> <!--appwidget是控制组件属性的那个xml --> <meta-data android:name="android.appwidget.provider" android:resource="@xml/appwidget" /> </receiver>
package org.lxh.activity; import android.app.Activity; import android.app.PendingIntent; import android.appwidget.AppWidgetManager; import android.appwidget.AppWidgetProvider; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.widget.RemoteViews; public class MyAppwidget extends AppWidgetProvider { public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { for(int i=0;i<appWidgetIds.length;i++){ //设置activity Intent intent=new Intent(context,AppwidgetActivity.class); PendingIntent pIntent=PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); //定义要操作的RemoteViews RemoteViews remote=new RemoteViews(context.getPackageName(),R.layout.appwidget); remote.setOnClickPendingIntent(R.id.img, pIntent); appWidgetManager.updateAppWidget(appWidgetIds[i], remote); } } }
package org.lxh.demo; import android.app.PendingIntent; import android.appwidget.AppWidgetManager; import android.appwidget.AppWidgetProvider; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.widget.RemoteViews; public class MyAppWidget extends AppWidgetProvider { @Override public void onReceive(Context context, Intent intent) { if ("org.lxh.action.MYAPPWIDGET_UPDATE".equals(intent.getAction())) { RemoteViews remote = new RemoteViews(context.getPackageName(), R.layout.mldn_appwidget); remote.setImageViewResource(R.id.img, R.drawable.mldn_man); remote.setTextViewText(R.id.but, "www.MLDNJAVA.cn") ; AppWidgetManager appWidgetManager = AppWidgetManager .getInstance(context); ComponentName componentName = new ComponentName(context, MyAppWidget.class); appWidgetManager.updateAppWidget(componentName, remote) ; } else { super.onReceive(context, intent) ; // 如果不写此代码,表示无法调用onUpdate() } } @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { Intent intent = new Intent(); intent.setAction("org.lxh.action.MYAPPWIDGET_UPDATE") ; PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); RemoteViews remote = new RemoteViews(context.getPackageName(), R.layout.mldn_appwidget); remote.setOnClickPendingIntent(R.id.but, pendingIntent); appWidgetManager.updateAppWidget(appWidgetIds, remote); } }
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="org.lxh.demo" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="10" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".MyAppWidgetDemo" 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=".MyAppwidget"> <intent-filter> <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> </intent-filter> <intent-filter> <action android:name="org.lxh.action.MYAPPWIDGET_UPDATE" /> </intent-filter> <meta-data android:name="android.appwidget.provider" android:resource="@xml/appwidget" /> </receiver> </application> </manifest>