package com.example.appwidget02;
import com.example.appwidget02.R.drawable;
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 appwidget02 extends AppWidgetProvider{ private static final String MY_ACTION="my.action.APPWIDGET_UPDATE";//静态全局常量 @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if(MY_ACTION.equals(action)){ RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.example_appwidgit); remoteViews.setImageViewResource(R.id.my_image, R.drawable.ic_launcher); AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context); ComponentName componentName = new ComponentName(context, appwidget02.class); appWidgetManager.updateAppWidget(componentName, remoteViews); }else{ super.onReceive(context, intent); } } @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { for(int i=0;i<appWidgetIds.length;i++){ Intent intent = new Intent(); intent.setAction(MY_ACTION); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0); RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.example_appwidgit); remoteViews.setOnClickPendingIntent(R.id.button1, pendingIntent); appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews); } super.onUpdate(context, appWidgetManager, appWidgetIds); } @Override public void onEnabled(Context context) { // TODO Auto-generated method stub super.onEnabled(context); } @Override public void onDisabled(Context context) { // TODO Auto-generated method stub super.onDisabled(context); } @Override public void onDeleted(Context context, int[] appWidgetIds) { // TODO Auto-generated method stub super.onDeleted(context, appWidgetIds); } }
配置文件
mainfest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.appwidget02" android:versionCode="1" android:versionName="1.0" >
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="14" />
<application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".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=".appwidget02" > <intent-filter> <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> </intent-filter> <intent-filter> <action android:name="my.action.APPWIDGET_UPDATE"/> </intent-filter> <meta-data android:name="android.appwidget.provider" android:resource="@xml/appwidget_info" /> </receiver> </application>
</manifest>
xml/example.xml
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="294dp"
android:minHeight="294dp"
android:updatePeriodMillis="86400000"
android:initialLayout="@layout/example_appwidgit"
>
</appwidget-provider>
layout/example_appwidget.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/textView01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="first widgit" android:background="#000000" /> <Button android:id="@+id/button1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="点击测试" /> <ImageView android:id="@+id/my_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dip" android:src="@drawable/yes" />
</LinearLayout>