An Intent is something that is used right now; a PendingIntent is something that may create an Intent in the future. You will use a PendingIntent with Notifications, AlarmManager, etc.
Notification n = new Notification(R.drawable.face_1, "Service啟動", System.currentTimeMillis()); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, TServiceHolder.class), 0); n.setLatestEventInfo(this, "任務標題", "任務內容", contentIntent); nManager.notify(NOTIFICATION_ID, n); // 任務欄啟動
String msg ="你好,美女"; String number = "187****5477"; SmsManager sms = SmsManager.getDefault(); PendingIntent pi = PendingIntent.getBroadcast(SmsActivity.this,0,new Intent(...),0); sms.sendTextMessage(number, null, msg, pi, null); //短信發送成功后要把intent廣播出去 Toast.makeText(SmsActivity.this,"發送成功",Toast.LENGHT_LONG).show();
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
函數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在進行被賦予的相關的操作。
==============================================================================
Intent 表示一個目的,第一個參數表示所在類,第二個參數表示目標類
PendingIntent 即是一個Intent的描述
PendingIntent和Intent的區別:
PendingIntent就是一個Intent的描述,我們可以把這個描述交給別的程序,別的程序根據這個描述在后面的別的時間做你安排做的事情
換種說法Intent 字面意思是意圖,即我們的目的,我們想要做的事情,在activity中,我們可以立即執行它
PendingIntent 相當於對intent執行了包裝,我們不一定一定要馬上執行它,我們將其包裝后,傳遞給其他activity或application
這時,獲取到PendingIntent 的application 能夠根據里面的intent 來得知發出者的意圖,選擇攔擊或者繼續傳遞或者執行