天天看点

Intent和PendingIntent的区别

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);  

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);  

代码解释 

      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在进行被赋予的相关的操作。