PendingIntent
来源:互联网 发布:软件人才缺口多少 编辑:程序博客网 时间:2024/05/24 06:58
PendingIntent和Intent的区别
pendingIntent翻译过来是未决定的、即将发生的Intent。与Intent的区别在于Intent的执行是立刻的,pendingIntent的执行是迟缓的。PendingIntent的获取
PendingIntent.getActivity(context, requestCode, intent, flags); 在满足条件会打开Activity
分别对应着Intent的3个行为,跳转到一个activity组件、打开一个广播组件和打开一个服务组件。
PendingIntent.getBroadcast(context, requestCode, intent, flags); 在满足条件会发送广播
PendingIntent.getService(context, requestCode, intent, flags); 在满足条件会打开服务
其中获取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 indicating that if the described PendingIntent already exists, the current one should be canceled before generating a new one.
指示如果描述的PendingIntent已经存在,则取消这个存在的,然后生成一个新的。与FLAG_UPDATE_CURRENT相反
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 参数介绍
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在发送成功和送达时,会发送两个广播,我们可以在代码中注册这个两个广播,来监听发送成功和送达的状态。
- PendingIntent
- PendingIntent
- PendingIntent
- PendingIntent
- PendingIntent
- PendingIntent
- PendingIntent
- PendingIntent
- pendingIntent
- PendingIntent
- PendingIntent
- pendingIntent
- pendingIntent
- PendingIntent
- PendingIntent
- PendingIntent
- pendingIntent
- PendingIntent
- HDU 2578 Dating with girls(1)
- IOS - 登录界面
- UVALive 6510 Stickers
- Python学习笔记-生成器
- JavaScript/jQuery WebIM 及时聊天通信工具 本地客户端
- PendingIntent
- 2012年5月SAT香港真题解析
- UINavigationController与UITabbarController的样式
- Struts2的ValueStack及EL表达式中取ValueStack的内幕
- Android 代码混淆
- 优化之误!
- 联合(union)用法
- Application.mk文件详解
- [转]JAVA学习路线及推荐书籍