歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android Intent和PendingIntent的區別詳細分析

Android Intent和PendingIntent的區別詳細分析

日期:2017/3/1 10:27:28   编辑:Linux編程

剛才一個例子中用到了PendingIntent,與之前學過的Intent有些類似,所以百度了一下結合自己的理解做個整理:

Intent是一個意圖,一個描述了想要啟動一個Activity、Broadcast或是Service的意圖。它主要持有的信息是它想要啟動的組件(Activity、Broadcast或是Service)。

PendingIntent可以看作是對Intent的包裝。供當前App之外的其他App調用。有點“被動”或是“Callback”的意思,但不是嚴格意義上的“被動”或是“Callback”。總之,當前App不能用它馬上啟動它所包裹的Intent。而是在外部App執行這個PendingIntent時,間接地、實際地調用裡面的Intent。PendingIntent主要持有的信息是它所包裝的Intent和當前App的Context。正由於PendingIntent中保存有當前App的Context,使它賦予外部App一種能力,使得外部App可以如同當前App一樣的執行PendingIntent裡的Intent,就算在執行時當前App已經不存在了,也能通過存在PendingIntent裡的Context照樣執行Intent。

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一個包裝。

  1. private void showNotify(){
  2. Notification notice=new Notification();
  3. notice.icon=R.drawable.icon;
  4. notice.tickerText=”您有一條新的信息”;
  5. notice.defaults=Notification.DEFAULT_SOUND;
  6. notice.when=10L;
  7. // 100 毫秒延遲後,震動 250 毫秒,暫停 100 毫秒後,再震動 500 毫秒
  8. //notice.vibrate = new long[] { 100, 250, 100, 500 };出錯?
  9. //notice.setLatestEventInfo(this, “通知”, “開會啦”, PendingIntent.getActivity(this, 0, null, 0));
  10. notice.setLatestEventInfo(this, “通知”, “開會啦”, PendingIntent.getActivity(this, 0, new Intent(this,Activity2.class), 0));//即將跳轉頁面,還沒跳轉
  11. NotificationManager manager=(NotificationManager)getSystemService(this.NOTIFICATION_SERVICE);
  12. manager.notify(0,notice);
  13. }

1. GSM網絡中Android發送短信示例

  1. String msg =”你好,Linux公社www.linuxidc.com”;
  2. String number = “135****6789″;
  3. SmsManager sms = SmsManager.getDefault();
  4. PendingIntent pi = PendingIntent.getBroadcast(SmsActivity.this,0,new Intent(…),0);
  5. sms.sendTextMessage(number, null, msg, pi, null);
  6. 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在進行被賦予的相關的操作。

Copyright © Linux教程網 All Rights Reserved