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一个包装。 java
- 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发送短信示例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();
代码解释
PendingIntent就是一个Intent的描述,咱们能够把这个描述交给别的程序,别的程序根据这个描述在后面的别的时间作你安排作的事情 (By giving a PendingIntent to another application, you are granting it the right to perform the operation you have specified as if the other application was yourself,就至关于PendingIntent表明了Intent)。本例中别的程序就是发送短信的程序,短信发送成功后要把intent广播出 去 。
函数SmsManager.sendTextMessage(String destinationAddress, String scAddress, String text, PendingIntent sentIntent, PendingIntent deliveryIntent)中参数解释:
1)PendingIntent sentIntent:当短信发出时,成功的话sendIntent会把其内部的描述的intent广播出去,不然产生错误代码并经过 android.app.PendingIntent.OnFinished进行回调,这个参数最好不为空,不然会存在资源浪费的潜在问题;
2)PendingIntent deliveryIntent:是当消息已经传递给收信人后所进行的PendingIntent广播。
查看PendingIntent 类能够看到许多的Send函数,就是PendingIntent在进行被赋予的相关的操做。网络