PendingIntent可以看作是对Intent的一个封装,但它不是立刻执行某个行为,而是满足某些条件或触发某些事件后才执行指定的行为。
PendingIntent的获取
PendingIntent获取有三种方式:通过Activity,Service,BroadcastReceiver获取.
1. 你可以通过getActivity(Context context, int requestCode, Intent intent, int flags)系列方法从系统 取得一个用于启动一个Activity的PendingIntent对象.
2.可以通过getService(Context context, int requestCode, Intent intent, int flags)方法从系统取得一个 用于启动一个Service的PendingIntent对象.
3.可以通过getBroadcast(Context context, int requestCode, Intent intent, int flags)方法从系统取得一个用于向BroadcastReceiver的发送广播的PendingIntent对象.
PendingIntent的参数说明
拿第三种方式,广播的形式说明下
PendingIntent sentIntent = PendingIntent.getBroadcast(this, 0,sIntent, 0);
第一个参数是上下文.
第二个参数是每次requestcode不同,就能产生多个Pendingintent.
第三个参数是用来存储信息.
第四个参数是对不同操作作标识.
getBroadcast(Context context, int requestCode, Intent intent, int flags)中的flags有几种状态:
1.FLAG_CANCEL_CURRENT:如果AlarmManager管理的PendingIntent已经存在,那么将会取消当前的PendingIntent,从而创建一个新的PendingIntent.
2.FLAG_UPDATE_CURRENT:如果AlarmManager管理的PendingIntent已经存在,让新的Intent更新之前Intent对象数据,
例如更新Intent中的Extras,另外,我们也可以在PendingIntent的原进程中调用PendingIntent的cancel ()把其从系统中移除掉
3.FLAG_NO_CREATE:如果AlarmManager管理的PendingIntent已经存在,那么将不进行任何操作,直接返回已经.
4.FLAG_ONE_SHOT:该PendingIntent只作用一次.在该PendingIntent对象通过send()方法触发过后,
PendingIntent将自动调用cancel()进行销毁,那么如果你再调用send()方法的话,系统将会返回一个SendIntentException.
PendingIntent举例
发送短信:
private final static String SEND_ACTION = "send"; private final static String DELIVERED_ACTION = "delivered"; private void sendSms(String receiver, String text) { SmsManager s = SmsManager.getDefault(); PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SEND_ACTION), PendingIntent.FLAG_CANCEL_CURRENT); PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent(DELIVERED_ACTION), PendingIntent.FLAG_CANCEL_CURRENT); // 发送完成 registerReceiver(new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { switch (getResultCode()) { case Activity.RESULT_OK: Toast.makeText(getBaseContext(), "Send Success!", Toast.LENGTH_SHORT).show(); break; case SmsManager.RESULT_ERROR_GENERIC_FAILURE: Toast.makeText(getBaseContext(), "Send Failed because generic failure cause.", Toast.LENGTH_SHORT).show(); break; case SmsManager.RESULT_ERROR_NO_SERVICE: Toast.makeText(getBaseContext(), "Send Failed because service is currently unavailable.", Toast.LENGTH_SHORT).show(); break; case SmsManager.RESULT_ERROR_NULL_PDU: Toast.makeText(getBaseContext(), "Send Failed because no pdu provided.", Toast.LENGTH_SHORT).show(); break; case SmsManager.RESULT_ERROR_RADIO_OFF: Toast.makeText(getBaseContext(), "Send Failed because radio was explicitly turned off.", Toast.LENGTH_SHORT).show(); break; default: Toast.makeText(getBaseContext(), "Send Failed.", Toast.LENGTH_SHORT).show(); break; } } }, new IntentFilter(SEND_ACTION)); // 对方接受完成 registerReceiver(new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { switch (getResultCode()) { case Activity.RESULT_OK: Toast.makeText(getBaseContext(), "Delivered Success!", Toast.LENGTH_SHORT).show(); break; default: Toast.makeText(getBaseContext(), "Delivered Failed!", Toast.LENGTH_SHORT).show(); break; } } }, new IntentFilter(DELIVERED_ACTION)); // 发送短信,sentPI和deliveredPI将分别在短信发送成功和对方接受成功时被广播 s.sendTextMessage(receiver, null, text, sentPI, deliveredPI); }
这里特别说明下:群发短信的时候,要保持每次发送的短信内容都不相同.可以将PendingIntent的第二个参数设置成不同的值,第四个参数设置成FLAG_ONE_SHOT.
这样就可以解决群发短信的两个问题:
1.群发短信时,短信内容取到的值是最后一条短信的内容.
2.群发短信时,短信内容总是不更新, 一成不变的问题.