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在進行被賦予的相關的操作。