【Android應用開發】Intent和PendingIntent的區別

來源:互聯網
上載者:User

標籤:

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

【Android應用開發】Intent和PendingIntent的區別

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.