在appWidget中,ImageButton和Button都是被支持的控件,其事件可分成三种类型:
一、开启Activity二、开始Service三、发送按钮Action下面开始一个一个分析,如何实现。一、开启Activity1、首先先定义个开启Activity的intenteg: Intent fullIntent=new Intent(this,FullScreen.class);若要传递数据,则使用intent.putExtra()方法eg: fullIntent.putExtra("isCircle",isCircle);2、用intent实例化一个PendingIntent,调用pendingIntent的getActicity方法来启动另一个Activity①若该Intent带有数据,则需要将最后一个参数的值设为:FLAG_CANCEL_CURRENTeg: PendingIntent Pfullintent=PendingIntent.getActivity(this, 0, fullIntent,PendingIntent.FLAG_CANCEL_CURRENT);②若该Intent不带数据,则最后一个参数设为0eg: PendingIntent Pfullintent=PendingIntent.getActivity(this, 0, fullIntent, 0);3、实例化RemoteView,其对应相应的Widget布局eg: RemoteViews views= newRemoteViews(getPackageName(), R.layout.widget);4、给RemoteView上的Button或ImageButton设置按钮事件eg: views.setOnClickPendingIntent(R.id.IBfullscreen,Pfullintent);5、更新AppWidget界面①如果是在onUpdate()方法内更新AppWidget界面eg: appWidgetManager.updateAppWidget(appWidgetIds, ActivityView);②如果是在onUpdate()方法外(一般为Service内)更新AppWidget界面,则需要定义几个变量eg: public RemoteViews views;//RemoteView对象 publicComponentName thisWidget; //组件名 public AppWidgetManager manager; // AppWidget管理器 thisWidget = new ComponentName(this,PictureAppWidgetProvider.class); manager = AppWidgetManager.getInstance(this); manager.updateAppWidget(thisWidget, views);二、开启Service1、定义一个intent来开启Serviceeg: Intent startServiceInten=newIntent("zyf.temp.Service.START");注:参数为开启Service的动作2、用Intent实例化一个PendingIntent,利用PendingIntent的getService方法来启动一个服务eg: PendingIntent Pintent=PendingIntent.getService(context, 0, startServiceInten, 0);3、实例化RemoteView,其对应相应的Widget布局eg: RemoteViews views= newRemoteViews(getPackageName(), R.layout.widget);4、给RemoteView上的Button或ImageButton设置按钮事件eg: views.setOnClickPendingIntent(R.id.IBfullscreen,Pfullintent);5、更新AppWidget界面①如果是在onUpdate()方法内更新AppWidget界面eg: appWidgetManager.updateAppWidget(appWidgetIds, ActivityView);②如果是在onUpdate()方法外(一般为Service内)更新AppWidget界面,则需要定义几个变量eg: public RemoteViews views;//RemoteView对象 publicComponentName thisWidget; //组件名 public AppWidgetManager manager; // AppWidget管理器 thisWidget = new ComponentName(this,PictureAppWidgetProvider.class); manager = AppWidgetManager.getInstance(this); manager.updateAppWidget(thisWidget, views);三、发送按钮Action1、定义一个Intent来发送按钮Actioneg: Intent prevInten=new Intent("PREV");2、用Intent实例化一个PendingIntent,利用PendingIntent的getBroadcast方法来发送广播eg: PendingIntent Pprevintent=PendingIntent.getBroadcast(this, 0, prevInten, 0);3、实例化RemoteView,其对应相应的Widget布局eg: RemoteViews views= newRemoteViews(getPackageName(), R.layout.widget);4、给RemoteView上的Button或ImageButton设置按钮事件eg: views.setOnClickPendingIntent(R.id.IBprev,Pprevintent);5、更新AppWidget界面①如果是在onUpdate()方法内更新AppWidget界面eg: appWidgetManager.updateAppWidget(appWidgetIds, ActivityView);②如果是在onUpdate()方法外(一般为Service内)更新AppWidget界面,则需要定义几个变量eg: public RemoteViews views;//RemoteView对象 publicComponentName thisWidget; //组件名 public AppWidgetManager manager; // AppWidget管理器 thisWidget = new ComponentName(this,PictureAppWidgetProvider.class); manager = AppWidgetManager.getInstance(this); manager.updateAppWidget(thisWidget, views);6、接收该Action①在AppWidget自己的onReceive方法内接收⒈在Action,要在Manifest.xml中加入Actioneg: <intent-filter> <actionandroid:name="android.appwidget.action.APPWIDGET_UPDATE"></action> <actionandroid:name="PREV"></action> </intent-filter>⒉在onReceive()方法内编写要实现的动作eg: if(intent.getAction().equals("PREV")){ //在这编写接收到该Action后要实现的动作}②在Service内接收⒈注册一个BroadcastReceive,声明接收器eg: IntentFilter filter=new IntentFilter(); filter.addAction("PREV"); registerReceiver(doCommand, filter);⒉,在BroadcastReceive类的onReceive方法内编写要实现的动作eg: if(intent.getAction().equals("PREV")){ //在这编写接收到该Action后要实现的动作}
转载自:http://www.eoeandroid.com/forum.php?mod=viewthread&tid=204378