AppWidget(二) PendingIntent

来源:互联网 发布:mini metro mac 编辑:程序博客网 时间:2024/06/06 03:57

      PendingIntent 待定的意图 我们可以同过它来启动其它应用的activity,service,broadcast,

PendingIntent  是系统用于检索原始数据的一个标记,即使一个application停止了,但如果又新建application  , 检索个相同intent的PendingIntent 它将和原来的PndingIntent 拥有相同的标记, 必须通过remove 移除它,否者可能会出现该应用出现上次应用的数据

    PendingIntent 可以通过getService,getActivity,getBroadcast 来启动相应的组件,

   getService(Context context, int requestCode,Intent intent, int flags)

   requestCode : 当需要发送不同的pendingIntent 的时候 必须设置该值,否者将会出现可能多个button 发送的是同一个intent事件

  flags 有4种形式 FLAG_CANCEL_CURRENT    取消当前的pendingIntent , 当只需要更新intent  时可以调用这个,但必须确保前面的么有需要的实体,否者前面的会失效,

 FLAG_UPDATE_CURRENT    更新所有的pendingIntent 比较实用

FLAG_NO_CREATE    不新建  如果不存在 返回null

FLAG_ONE_SHOT   只新建一次 当触发send时  自动清除pendingIntent

Mp3 中多个button绑定pendingIntent 事件

@Overridepublic void onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds) {System.out.println("onUpdate");for(int i=0;i<appWidgetIds.length;i++){//创建一个Intent对象RemoteViews remoteViews=new RemoteViews(context.getPackageName(),R.layout.appwidget_music);//前一首歌Intent intent=new Intent( MUSIC_PLAY_STATE);Bundle bundle=new Bundle();bundle.putString("action","prev");bundle.putInt(AppWidgetManager.EXTRA_APPWIDGET_ID,appWidgetIds[i]);intent.putExtras(bundle);// 创建一个PendingIntent对象PendingIntent pendingIntent=PendingIntent.getBroadcast(context,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);remoteViews.setOnClickPendingIntent(R.id.prevMusicImage,pendingIntent);//下一首intent=new Intent( MUSIC_PLAY_STATE);bundle=intent.getExtras();bundle=new Bundle();bundle.putString("action","next");bundle.putInt(AppWidgetManager.EXTRA_APPWIDGET_ID,appWidgetIds[i]);intent.putExtras(bundle); pendingIntent=PendingIntent.getBroadcast(context,1,intent,PendingIntent.FLAG_UPDATE_CURRENT);remoteViews.setOnClickPendingIntent(R.id.nextMusicImage,pendingIntent);//播放或暂停intent=new Intent( MUSIC_PLAY_STATE);bundle=new Bundle();bundle.putString("action","playOrPause");intent.putExtras(bundle);pendingIntent=PendingIntent.getBroadcast(context,2,intent,PendingIntent.FLAG_UPDATE_CURRENT);remoteViews.setOnClickPendingIntent(R.id.playOrPauseImage,pendingIntent);System.out.println("appWidgetIds======"+appWidgetIds[i]);appWidgetManager.updateAppWidget(appWidgetIds[i],remoteViews);}super.onUpdate(context, appWidgetManager, appWidgetIds);}


 

原创粉丝点击