PendingIntent

来源:互联网 发布:汉字注音软件 编辑:程序博客网 时间:2024/05/16 14:41

PendingIntent用于描述Intent及其最终的行为.你可以通过getActivity(Context context, int requestCode, Intent intent, intflags)系列方法从系统取得一个用于启动一个Activity的PendingIntent对象, 可以通过getService(Contextcontext, int requestCode, Intent intent, intflags)方法从系统取得一个用于启动一个Service的PendingIntent对象,可以通过getBroadcast(Context context,int requestCode, Intent intent, intflags)方法从系统取得一个用于向BroadcastReceiver的Intent广播的PendingIntent对象。返回的PendingIntent可以递交给别的应用程序,然后继续处理。

 

       当你把PendingIntent递交给别的程序进行处理时,PendingIntent仍然拥有PendingIntent原程序所拥有的权限(withthe same permissions andidentity).当你从系统取得一个PendingIntent时,一定要非常小心才行。比如,通常,如果Intent目的地是你自己的component(Activity/Service/BroadcastReceiver)的话,你最好采用在Intent中显示指定目的component名字的方式,以确保Intent最终能发到目的,否则Intent最后可能不知道发到哪里了。一个PendingIntent就是Android系统中的一个token(节点,这个应该是Linux或C\C++用语)的一个对象引用,它描述了一些将用于retrieve的数据(这里,这些数据描述了Intent及其最终的行为)。

 

        即使PendingIntent原进程结束了的话,PendingIntent本身仍然还存在,可在其他进程(PendingIntent被递交到的其他程序)中继续使用.如果我在从系统中提取一个PendingIntent的,而系统中有一个和你描述的PendingIntent对等的PendingInent(对于程序来讲,就是通过同样的方法获取的,例如都是通过getActivity、getBroadcast、getService方法来获取的,并且传递给getXXX方法的Intent对象的Action是相同的,Data也是相同的,Categories也是相同的,Components也是相同的,Flags也是相同的),那么系统会直接返回和该PendingIntent其实是同一token的PendingIntent,而不是一个新的token和PendingIntent。然而你在从提取PendingIntent时,通过FLAG_CANCEL_CURRENT参数,让这个老PendingIntent的先cancel()掉,这样得到的pendingInten和其token的就是新的了。

 

当有相同的PendingIntent发过来时,它会进行比较,如果感觉是same的话就会沿用原来的PendingIntent,所以这里经常会出现不能更新信息的问题。为此,需要设置flag

PendingIntent下有4个flags:

 

FLAG_CANCEL_CURRENT:

这个意思很明确了,当有新的PendingIntent过来的时候,首先cancel掉原来的PendingIntent,然后设置新的PendingIntent,注意它最后一句话:thisensures that only entities given the new data will be able to launch it代表了newdata会被强制更新进来

 

 

 

FLAG_NO_CREATE:

对于默认的设置来说,当给进来的PendingIntent不存在时,会自动创建一个新的,这个flag否定了这个设置,也就是说它只会利用已经存在的PendingIntent而没有创造的能力,这对于控制权限和处理一些安全问题是有好处的。

 

 

 

FLAG_ONE_SHOT:

前面我们说过,PendingIntent启动之后不会被Kill掉,但是这里设置了FLAG_ONE_SHOT之后表明它运行之后就被Kill掉了,这方面跟Intent就一样了

 

 

 

FLAG_UPDATE_CURRENT:

这个Flag对应FLAG_CANCEL_CURRENT存在,它不会消除之前的Intent,只是更新extradata。这样在不关心Intent被谁接受的前提下,用这个Flag能够提高优化的效率

 

 

默认的设置,新进来的PendingIntent如果与原来存在的PendingIntent比较,被认为是same的时候会抛弃新进来的PendingIntent,这就是为什么默认设置下extradata不会被更新的原因。

 

最后,有一点需要特别说明,flag是可以或的。个人比较推荐PendingIntent.FLAG_UPDATE_CURRENT| PendingIntent.FLAG_ONE_SHOT的搭配。

 

 

PendingIntent经常与Notification配合,实现通知栏点击后行为的定义。

 

[java] view plaincopy

Intent intent = newIntent(TestPendingIntentActivity.this, TargetActivity.class); 

PendingIntentpendingIntent = PendingIntent.getActivity(TestPendingIntentActivity.this, 0,intent, 0); 

Notificationnotification = new Notification(R.drawable.icon, "notification",System.currentTimeMillis()); 

notification.setLatestEventInfo(TestPendingIntentActivity.this,"TestPendingIntent", "Testing...", pendingIntent); 

NotificationManagernotificationManager =(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); 

notificationManager.notify(0,notification); 

 

 

在这里例子里面,Notification启动就已经在外部开始执行PendingIntent,当你点击Notification的时候,会发生跳转,screen会去到Intent指向的地方。在这里要注意requestCode,当有很多个Notification的时候,作为一个独立的notificationinstance你必须拥有独立的requestCode,所以建议把所有的notification id与PendingIntentrequestCode对应起来,否则很一疏忽很容易就被相同的requestCode的PendingIntent覆盖掉

0 0
原创粉丝点击