Intent和PendingIntent的区别 http://blog.youkuaiyun.com/zeng622peng/article/details/6180190
intent英文意思是意图,pending表示即将发生或来临的事情。
PendingIntent这个类用于处理即将发生的事情。比如在通知Notification中用于跳转页面,但不是马上跳转。
Intent 是及时启动,intent 随所在的activity 消失而消失。
PendingIntent 可以看作是对intent的包装,通常通过getActivity,getBroadcast ,getService来得到pendingintent的实例,当前activity并不能马上启动它所包含的intent,而是在外部执行 pendingintent时,调用intent的。正由于pendingintent中 保存有当前App的Context,使它赋予外部App一种能力,使得外部App可以如同当前App一样的执行pendingintent里的 Intent, 就算在执行时当前App已经不存在了,也能通过存在pendingintent里的Context照样执行Intent。另外还可以处理intent执行后的操作。常和alermanger 和notificationmanager一起使用。
Intent一般是用作Activity、Sercvice、BroadcastReceiver之间传递数据,而Pendingintent,一般用在 Notification上,可以理解为延迟执行的intent,PendingIntent是对Intent一个包装。
- private
void showNotify(){ -
Notification notice=new Notification(); -
notice.icon=R.drawable.icon; -
notice.tickerText="您有一条新的信息"; -
notice.defaults=Notification.DEFAULT_SOUND; -
notice.when=10L; -
// 100 毫秒延迟后,震动 250 毫秒,暂停 100 毫秒后,再震动 500 毫秒 -
//notice.vibrate = new long[] { 100, 250, 100, 500 };出错? -
//notice.setLatestEventInfo(this, "通知", "开会啦", PendingIntent.getActivity(this, 0, null, 0)); - notice.setLatestEventInfo(this,
"通知", "开会啦", PendingIntent.getActivity(this, 0, new Intent(this,Activity2.class), 0));//即将跳转页面,还没跳转 -
NotificationManager manager=(NotificationManager)getSystemService(this.NOTIFICATION_SERVICE); -
manager.notify(0,notice); -
}
- private
void showNotify(){ -
Notification notice=new Notification(); -
notice.icon=R.drawable.icon; -
notice.tickerText="您有一条新的信息"; -
notice.defaults=Notification.DEFAULT_SOUND; -
notice.when=10L; -
// 100 毫秒延迟后,震动 250 毫秒,暂停 100 毫秒后,再震动 500 毫秒 -
//notice.vibrate = new long[] { 100, 250, 100, 500 };出错? -
//notice.setLatestEventInfo(this, "通知", "开会啦", PendingIntent.getActivity(this, 0, null, 0)); - notice.setLatestEventInfo(this,
"通知", "开会啦", PendingIntent.getActivity(this, 0, new Intent(this,Activity2.class), 0));//即将跳转页面,还没跳转 -
NotificationManager manager=(NotificationManager)getSystemService(this.NOTIFICATION_SERVICE); -
manager.notify(0,notice); -
}
private void showNotify(){
Notification notice=new Notification();
notice.icon=R.drawable.icon;
notice.tickerText="您有一条新的信息";
notice.defaults=Notification.DEFAULT_SOUND;
notice.when=10L;
// 100 毫秒延迟后,震动 250 毫秒,暂停 100 毫秒后,再震动 500 毫秒
//notice.vibrate = new long[] { 100, 250, 100, 500 };出错?
//notice.setLatestEventInfo(this, "通知", "开会啦", PendingIntent.getActivity(this, 0, null, 0));
notice.setLatestEventInfo(this, "通知", "开会啦", PendingIntent.getActivity(this, 0, new Intent(this,Activity2.class), 0));//即将跳转页面,还没跳转
NotificationManager manager=(NotificationManager)getSystemService(this.NOTIFICATION_SERVICE);
manager.notify(0,notice);
}
1. GSM网络中android发送短信示例
- String
msg ="你好,美女"; - String
number = "135****6784"; - SmsManager
sms = SmsManager.getDefault(); -
- PendingIntent
pi = PendingIntent.getBroadcast(SmsActivity.this,0,new Intent(...),0); - sms.sendTextMessage(number,
null, msg, pi, null); - Toast.makeText(SmsActivity.this,"发送成功",Toast.LENGHT_LONG).show();
- String
msg ="你好,美女"; - String
number = "135****6784"; - SmsManager
sms = SmsManager.getDefault(); -
- PendingIntent
pi = PendingIntent.getBroadcast(SmsActivity.this,0,new Intent(...),0); - sms.sendTextMessage(number,
null, msg, pi, null); - Toast.makeText(SmsActivity.this,"发送成功",Toast.LENGHT_LONG).show();
String msg ="你好,美女"; String number = "135****6784"; SmsManager sms = SmsManager.getDefault(); PendingIntent pi = PendingIntent.getBroadcast(SmsActivity.this,0,new Intent(...),0); sms.sendTextMessage(number, null, msg, pi, null); Toast.makeText(SmsActivity.this,"发送成功",Toast.LENGHT_LONG).show();
代码解释
本文详细阐述了Android开发中Intent与PendingIntent的概念及其区别。Intent主要用于不同组件间的数据传递,而PendingIntent则用于延迟执行Intent,常用于Notification场景。
5285

被折叠的 条评论
为什么被折叠?



