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