AppWidget进阶篇


在appWidget中事件可分成三种类型
a、开启Activity
b、开始Service
c、发送按钮Action
下面开始一个一个分析,如何实现。


2.1、开启Activity

2.11、首先先定义个开启Activity的intent
eg:IntentfullIntent=newIntent(this,FullScreen.class);
若要传递数据,则使用intent.putExtra()方法
eg:fullIntent.putExtra(“isCircle”,isCircle);

2.12、用intent实例化一个PendingIntent,调用pendingIntent的getActicity方法来启动另一个Activity
①若该Intent带有数据,则需要将最后一个参数的值设为:FLAG_CANCEL_CURREN
eg:PendingIntentPfullintent=PendingIntent.getActivity(this,0,fullIntent,PendingIntent.FLAG_CANCEL_CURRENT);
②若该Intent不带数据,则最后一个参数设为0
eg:PendingIntentPfullintent=PendingIntent.getActivity(this,0,fullIntent,0);

2.13、实例化RemoteView,其对应相应的Widget布局
eg:RemoteViewsviews=newRemoteViews(getPackageName(),R.layout.widget);

2.14、给RemoteView上的Button或ImageButton设置按钮事件
eg:views.setOnClickPendingIntent(R.id.IBfullscreen,Pfullintent);

2.15、更新AppWidget界面
①如果是在onUpdate()方法内更新AppWidget界面
eg:appWidgetManager.updateAppWidget(appWidgetIds,ActivityView);
②如果是在onUpdate()方法外(一般为Service内)更新AppWidget界面,则需要定义几个变量

eg:publicRemoteViewsviews;RemoteView对象
publicComponentNamethisWidget;
组件名
publicAppWidgetManagermanager;AppWidget管理器

thisWidget=newComponentName(this,PictureAppWidgetProvider.class);
manager=AppWidgetManager.getInstance(this);
manager.updateAppWidget(thisWidget,views);


2.2、开启Service

2.21、定义一个intent来开启Service
eg:IntentstartServiceInten=newIntent(“zyf.temp.Service.START”);
注:参数为开启Service的动作

2.22、用Intent实例化一个PendingIntent,利用PendingIntent的getService方法来启动一个服务
eg:PendingIntentPintent=PendingIntent.getService(context,0,startServiceInten,0);

2.23、实例化RemoteView,其对应相应的Widget布局
eg:RemoteViewsviews=newRemoteViews(getPackageName(),R.layout.widget);

2.24、给RemoteView上的Button或ImageButton设置按钮事件
eg:views.setOnClickPendingIntent(R.id.IBfullscreen,Pfullintent);

2.25、更新AppWidget界面
①如果是在onUpdate()方法内更新AppWidget界面
eg:appWidgetManager.updateAppWidget(appWidgetIds,ActivityView);
②如果是在onUpdate()方法外(一般为Service内)更新AppWidget界面,则需要定义几个变量

eg:publicRemoteViewsviews;RemoteView对象
publicComponentNamethisWidget;
组件名
publicAppWidgetManagermanager;AppWidget管理器

thisWidget=newComponentName(this,PictureAppWidgetProvider.class);
manager=AppWidgetManager.getInstance(this);
manager.updateAppWidget(thisWidget,views);


2.3、发送按钮Action

2.31、定义一个Intent来发送按钮Action
eg:IntentprevInten=newIntent(“PREV”);

或者IntentprevIntent=newIntent(Context,CalendarAppWidgetProvider.Class)

prevIntent.setAction(“PREV”);(当开启一个service时,在service发送广播)

2.32、用Intent实例化一个PendingIntent,利用PendingIntent的getBroadcast方法来发送广播
eg:PendingIntentPprevintent=PendingIntent.getBroadcast(this,0,prevInten,0);

2.33、实例化RemoteView,其对应相应的Widget布局
eg:RemoteViewsviews=newRemoteViews(getPackageName(),R.layout.widget);

2.34、给RemoteView上的Button或ImageButton设置按钮事件
eg:views.setOnClickPendingIntent(R.id.IBprev,Pprevintent);

2.35、更新AppWidget界面
①如果是在onUpdate()方法内更新AppWidget界面
eg:appWidgetManager.updateAppWidget(appWidgetIds,ActivityView);
②如果是在onUpdate()方法外(一般为Service内)更新AppWidget界面,则需要定义几个变量

eg:publicRemoteViewsviews;RemoteView对象
publicComponentNamethisWidget;
组件名
publicAppWidgetManagermanager;AppWidget管理器

thisWidget=newComponentName(this,PictureAppWidgetProvider.class);
manager=AppWidgetManager.getInstance(this);
manager.updateAppWidget(thisWidget,views);

2.36、接收该Action
①在AppWidget自己的onReceive方法内接收
⒈在Action,要在Manifest.xml中加入Action
eg:<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:IntentFilterfilter=newIntentFilter();
filter.addAction(“PREV”);
registerReceiver(doCommand,filter);

⒉,在BroadcastReceive类的onReceive方法内编写要实现的动作
eg:if(intent.getAction().equals(“PREV”))
{
在这编写接收到该Action后要实现的动作
}




更多相关文章

  1. Android(安卓)之最新最全的Intent传递数据方法
  2. Android(安卓)OnClickListener 使用总结
  3. gradle 指定导出包的名字和路径
  4. android Activity runOnUiThread() 方法使用
  5. Android(安卓)Camera2 之 CameraCaptureSession 详解
  6. Android腾讯微薄客户端开发九:博主详情界面篇(广播,听众,收听)
  7. 【Android(安卓)Developers Training】 17. 停止和重启一个Activ
  8. Android-- ArrayAdapter用法举例
  9. android CountDownTimer

随机推荐

  1. android操作XML的几种方式
  2. Android(安卓)sensor hal 详解
  3. Android刷Root方法,zergRush,Odin3+CWM(Cl
  4. 【Android开发必备】Android APP开发经验
  5. android中的一些特性
  6. Adnroid开发中ListView使用SimpleAdapter
  7. Android LinearLayout 绾挎€у竷灞€
  8. Android常用的输入框
  9. Android中ScrollView嵌套ListView只显示
  10. android UI横竖屏的处理