本文实现Widget中的按钮点击事件,点击一次下面的按钮,上面的数字减少1。
首先是Manifest文件:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.widget_sample" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <receiver android:name=".MainActivity" android:label="WIDGET_SAMPLE"> <intent-filter> <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> </intent-filter> <meta-data android:name="android.appwidget.provider" android:resource="@xml/my_widget" /> </receiver> <service android:name="MainActivity$MyService" > <intent-filter> <action android:name="Widget.Button.Click"></action> </intent-filter> </service> </application> </manifest>
需要注意的是在MainActivity$MyService中定义的<intent-filter>,其中的Widget.Button.Click是自己定义的Action,如果没有加上这个Action,就无法收到点击按钮时发出的Action,也就不会更新Widget。
Java文件:
package com.example.widget_sample; import java.util.Timer; import android.app.PendingIntent; import android.app.Service; import android.appwidget.AppWidgetManager; import android.appwidget.AppWidgetProvider; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.os.IBinder; import android.util.Log; import android.widget.RemoteViews; public class MainActivity extends AppWidgetProvider{ public Context context; static int num = 100; static String tag_action = "Widget.Button.Click"; public void onUpdate(Context con, AppWidgetManager appWidgetManager, int[] appWidgetIds) { context = con; Intent intent = new Intent(context, MyService.class); context.startService(intent); } //MyService服务程序 public static class MyService extends Service { public void onStart(Intent intent, int startId){ ComponentName thisWidget = new ComponentName(this, MainActivity.class); AppWidgetManager manager = AppWidgetManager.getInstance(this); RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.activity_main); // 点击按钮时 if (intent.getAction() != null){ if (intent.getAction().equals(tag_action)){ num--; } } remoteViews.setTextViewText(R.id.textView1, String.valueOf(num)); // 定义一个Intent来发送按钮Action Intent prevInten = new Intent(); prevInten.setAction(tag_action); // 用Intent实例化一个PendingIntent PendingIntent Pprevintent=PendingIntent.getService(this, 0, prevInten, 0); // 给RemoteView上的Button设置按钮事件 remoteViews.setOnClickPendingIntent(R.id.button1, Pprevintent); manager.updateAppWidget(thisWidget, remoteViews); } @Override public IBinder onBind(Intent arg0) { // TODO Auto-generated method stub return null; } } }
首先,定义一个Intent,Action设置为"Widget.Button.Click",然后再用该Intent实例化一个PendingIntent,用RemoteViews.setOnClickPendingIntent()函数将PendingIntent和点击按钮的事件关联起来,最后更新Widget。
服务onStart()时,用Intent.getAction()函数得到Intent的Action,并判断是否是"Widget.Button.Click",如果是,表明这是单击按钮发出的Intent,num--,然后再更新Widget。这样就实现了单击按钮一次,num的值减少1。