本文实现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来发送按钮ActionIntent prevInten = new Intent();prevInten.setAction(tag_action);// 用Intent实例化一个PendingIntentPendingIntent Pprevintent=PendingIntent.getService(this, 0, prevInten, 0);// 给RemoteView上的Button设置按钮事件remoteViews.setOnClickPendingIntent(R.id.button1, Pprevintent);manager.updateAppWidget(thisWidget, remoteViews);}@Overridepublic IBinder onBind(Intent arg0) {// TODO Auto-generated method stubreturn 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。

更多相关文章

  1. Android常用的Gradle配置和加速编译
  2. ExpandableListView 和 ExpandableListActivity的使用及数据更新
  3. Android仿微信底部按钮滑动变色
  4. android MotionEvent详解
  5. Android视频播放项目总结之 使用VideoView定义自己的视频播放器
  6. 贴一个定义android animation的应用实例
  7. Animation(2、帧动画)
  8. android viewpage的施用
  9. ProgressBar自定义样式中的层叠图片显示问题

随机推荐

  1. Android可伸缩布局-FlexboxLayout(支持Rec
  2. android设置对话框背景透明度和弹出位置
  3. Google 究竟是不是要用 Fuchsia OS 取代
  4. 【Android】隐藏底部虚拟按键,亲测可用
  5. Android(安卓)如何 绘制View
  6. Android系统给第三方app签名流程
  7. android studio 启动报nexpected excepti
  8. android Paint 设置线宽setStrokeWidth()
  9. android获取mac地址方法
  10. Android 之通知Notification应用