android之PendingIntent

pendingIntent必须要满足一定条件下,才会触发放在里面的intent操作。可以在程序外部进行执行。即使是程序已经退出了。

在Android中,我们常常使用PendingIntent来表达一种“留待日后处理”的意思。从这个角度来说,PendingIntent可以被理解为一种特殊的异步处理机制。不过,单就命名而言,PendingIntent其实具有一定误导性,因为它既不继承于Intent,也不包含Intent,它的核心可以粗略地汇总成四个字——“异步激发”。
很明显,这种异步激发常常是要跨进程执行的。比如说A进程作为发起端,它可以从系统“获取”一个PendingIntent,然后A进程可以将PendingIntent对象通过binder机制“传递”给B进程,再由B进程在未来某个合适时机,“回调”PendingIntent对象的send()动作,完成激发。

PendingIntent 是 Android 提供的一种用于外部程序调起自身程序的能力,生命周期不与主程序相关。外部程序通过 PendingIntent 只能调用起三种组件:

  • Activity
  • Service
  • Broadcast

PendingIntent 的使用场景有三个:

  • 使用 AlarmManager 设定闹钟
  • 在系统状态栏显示 Notification
  • 在桌面显示 Widget

PendingIntent 也只能通过下列的静态方法获取:

// 获取 Broadcast 关联的 PendingIntent
PendingIntent.getBroadcast(Context context, int requestCode, Intent intent, int flags)

// 获取 Activity 关联的 PendingIntent
PendingIntent.getActivity(Context context, int requestCode, Intent intent, int flags)
PendingIntent.getActivity(Context context, int requestCode, Intent intent, int flags, Bundle options)

// 获取 Service 关联的 PendingIntent
PendingIntent.getService(Context context, int requestCode, Intent intent, int flags)
上面的 getActivity()的意思其实是,获取一个PendingIntent对象,而且该对象日后激发时所做的事情是启动一个新activity。也就是说,当它异步激发时,会执行类似Context.startActivity()那样的动作。 相应地,getBroadcast()和getService()所获取的PendingIntent对象在激发时,会分别执行类似Context..sendBroadcast()和Context.startService()这样的动作。

PendingIntent 是系统对于待处理数据的一个引用,称之为:token; 当主程序被 Killed 时,token 还是会继续存在的,可以继续供其他进程使用 。如果要取消 PendingIntent,需要调用PendingIntent 的 cancel 方法。

对于 PendingIntent 容易误解的一点是:
如果创建了很多 PendingIntent,只要 extra 中的数据不同的话,以为就是两个不同的 PendingIntent 这种理解是错误的!!Extras不参与Intent的匹配过程。

正确区分不同 PendingIntent 有两种方法:

  • PendingIntent.getXXX(…) 方法中的 requestCode 不同
  • 通过 Intent.filterEquals 测试时不相等

关于 PendingIntent.getXXX(…) 方法中第四个参数 flags,在 PendingIntent 定义了四个比较常用的 FLAG:

//如果新请求的 PendingIntent 发现已经存在时,取消已存在的,用新的 PendingIntent 替换
int FLAG_CANCEL_CURRENT

//如果新请求的 PendingIntent 发现已经存在时,忽略新请求的,继续使用已存在的。日常开发中很少使用
int FLAG_NO_CREATE

//表示 PendingIntent 只能使用一次,如果已使用过,那么 getXXX(...) 将会返回 NULL 
//也就是说同类的通知只能使用一次,后续的通知单击后无法打开。
int FLAG_ONE_SHOT

//如果新请求的 PendingIntent 发现已经存在时, 如果 Intent 有字段改变了,这更新已存在的 PendingIntent
int FLAG_UPDATE_CURRENT

主要的地方:Notification,SmsManager,AlarmManager等

1.Notification例子:

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
mBuilder.setSmallIcon(R.drawable.notification_icon)
mBuilder.setContentTitle("My notification")
mBuilder.setContentText("Hello World!");
Intent resultIntent = new Intent(this, ResultActivity.class);
PendingIntent resultPendingIntent = PendingIntent.getActivity(this,0,resultIntent,PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
int mNotificationId = 001;
// Gets an instance of the NotificationManager service
NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Builds the notification and issues it.
mNotifyMgr.notify(mNotificationId, mBuilder.build());
2.SmsManager例子:
SmsManager smsManage = SmsManager.getDefault();
Intent intent=new Intent("SEND_SMS_ACTION");
PendingIntent pendingIntent=PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0);
smsManage.sendTextMessage("13xxxxxxxxx", null, "这是一条短信", pendingIntent, null);





猜你喜欢

转载自blog.csdn.net/suyimin2010/article/details/80872817