PendingIntent

来源:互联网 发布:软件人才缺口多少 编辑:程序博客网 时间:2024/05/24 06:58
PendingIntent和Intent的区别
pendingIntent翻译过来是未决定的、即将发生的Intent。与Intent的区别在于Intent的执行是立刻的,pendingIntent的执行是迟缓的。

最大的区别是intent 随所在的activity 消失而消失。 而PendingIntent中保存有一个Context就算在执行时当前Application已经不存在了,也能通过存在PendingIntent里的Context去执行Intent
PendingIntent的获取

PendingIntent.getActivity(context, requestCode, intent, flags); 在满足条件会打开Activity
PendingIntent.getBroadcast(context, requestCode, intent, flags); 在满足条件会发送广播
PendingIntent.getService(context, requestCode, intent, flags);  在满足条件会打开服务
分别对应着Intent的3个行为,跳转到一个activity组件、打开一个广播组件和打开一个服务组件。


其中获取PendingIntent有4个参数:

context: 上下文

requestCode: 用于标识当前的PendingIntent。不同的requestCode代表不同的PenddingIntent。http://www.cnblogs.com/fengzhblog/archive/2013/07/21/3203363.html证实了这一点

intent :所包裹的intent,可以存放extra。

Flag:pendingIntent的行为描述:


FLAG_UPDATE_CURRENT:

Flag indicating that if the described PendingIntent already exists, then keep it but replace its extra data with what is in this new Intent  
指示如果两个PendingIntent一样的话,只保持第一个,第二个PendingIntent不会生效,仅仅会更新第一个PendingIntent的extra值。


FLAG_ONE_SHOT:

Flag indicating that this PendingIntent can be used only once.      
指示当前Pending只有第一次有效。


FLAG_CANCEL_CURRENT:

Flag indicating that if the described PendingIntent already exists, the current one should be canceled before generating a new one. 
指示如果描述的PendingIntent已经存在,则取消这个存在的,然后生成一个新的。FLAG_UPDATE_CURRENT相反


FLAG_NO_CREATE:
Flag indicating that if the described PendingIntent does not already exist, then simply return null instead of creating it.  
如果要创建的PendingIntent,在系统中已经不存在(被消耗了或压根没生成过),就返回null而不再创建这个PendingIntent。

关于怎么判定两个PedningIntent是否一样:
需要判断两个参数,requestCode 和 Intent。而判断Intent是否相等,需要参考action、category、data、flags、cmp等是否相等。而intent的extra值并不会影响Intent是否相等。也就是前几个参数都相等时,才会判断PendingIntent相等。


PendingIntent在闹钟中的参数设置

设置闹钟的PendingIntent需要从广播获取
pendingIntent = PendingIntent.getBroadcast(context, requestCode, intent, flags);

alarm.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAtMillis, pendingIntent);
alarm.cancel(pendingIntent);

当FLAG设为FLAG_UPDATE_CURRENT时,只要两个PendingIntent相等,后面再用这个PendingIntent去设置闹钟,则后一个PedningIntent的extra值会替换掉前一个PendingIntent的前一个值。如果闹钟时间不一样,就相当于开了两个闹钟。

其他的FLAG可以参考上面的FLAG 参数介绍


cancle为取消闹钟,同理,只有当取消闹钟的PendingIntent和设置闹钟的PendingIntent一样时,才能取消成功。不过如果设置闹钟的PendingIntent的FLAG设为FLAG_ONE_SHOT时,则cancle方法都不会起作用。


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);
}
来源: <http://blog.csdn.net/yuzhiboyi/article/details/8484771>

从代码中可以看出,在发短信过程中,PendingIntent在发送成功和送达时,会发送两个广播,我们可以在代码中注册这个两个广播,来监听发送成功和送达的状态。

 

















0 0
原创粉丝点击