Android PendingIntent介紹

一、PendingIntent做用 android

根據字面意思就知道是延遲的intent,主要用來在某個事件完成後執行特定的Action。PendingIntent包含了Intent及Context,因此就算Intent所屬程序結束,PendingIntent依然有效,能夠在其餘程序中使用。
經常使用在通知欄及短信發送系統中。 ide

PendingIntent通常做爲參數傳給某個實例,在該實例完成某個操做後自動執行PendingIntent上的Action,也能夠經過PendingIntent的send函數手動執行,並能夠在send函數中設置OnFinished表示send成功後執行的動做。 函數

二、PendingIntent舉例 this

a. 系統通知欄 spa

複製代碼
NotificationManager nm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); int icon = android.R.drawable.stat_notify_chat; long when = System.currentTimeMillis() + 2000; Notification n = new Notification(icon, "通知欄demo提醒", when); n.defaults = Notification.DEFAULT_SOUND; n.flags |= Notification.FLAG_AUTO_CANCEL; Intent openintent = new Intent(this, DemoList.class); PendingIntent pi = PendingIntent.getActivity(this, 0, openintent, PendingIntent.FLAG_CANCEL_CURRENT); n.setLatestEventInfo(this, "通知欄demo提醒title", "通知欄demo提醒text", pi); nm.notify(0, n);
複製代碼

setLatestEventInfo表示設置點擊該通知的事件 .net

b. 短信系統舉例 code

複製代碼
private final static String SEND_ACTION      = "send"; private final static String DELIVERED_ACTION = "delivered"; private void sendSms(String receiver, String text) { SmsManager s = SmsManager.getDefault(); PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SEND_ACTION), PendingIntent.FLAG_CANCEL_CURRENT); PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent(DELIVERED_ACTION), PendingIntent.FLAG_CANCEL_CURRENT); // 發送完成 registerReceiver(new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { switch (getResultCode()) { case Activity.RESULT_OK: Toast.makeText(getBaseContext(), "Send Success!", Toast.LENGTH_SHORT).show(); break; case SmsManager.RESULT_ERROR_GENERIC_FAILURE: Toast.makeText(getBaseContext(), "Send Failed because generic failure cause.", Toast.LENGTH_SHORT).show(); break; case SmsManager.RESULT_ERROR_NO_SERVICE: Toast.makeText(getBaseContext(), "Send Failed because service is currently unavailable.", Toast.LENGTH_SHORT).show(); break; case SmsManager.RESULT_ERROR_NULL_PDU: Toast.makeText(getBaseContext(), "Send Failed because no pdu provided.", Toast.LENGTH_SHORT).show(); break; case SmsManager.RESULT_ERROR_RADIO_OFF: Toast.makeText(getBaseContext(), "Send Failed because radio was explicitly turned off.", Toast.LENGTH_SHORT).show(); break; default: Toast.makeText(getBaseContext(), "Send Failed.", Toast.LENGTH_SHORT).show(); break; } } }, new IntentFilter(SEND_ACTION)); // 對方接受完成 registerReceiver(new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { switch (getResultCode()) { case Activity.RESULT_OK: Toast.makeText(getBaseContext(), "Delivered Success!", Toast.LENGTH_SHORT).show(); break; default: Toast.makeText(getBaseContext(), "Delivered Failed!", Toast.LENGTH_SHORT).show(); break; } } }, new IntentFilter(DELIVERED_ACTION)); // 發送短信,sentPI和deliveredPI將分別在短信發送成功和對方接受成功時被廣播 s.sendTextMessage(receiver, null, text, sentPI, deliveredPI); }
複製代碼

以上的兩個PendingIntent sentPI和deliveredPI將分別在短信發送成功和對方接受成功時被廣播 事件

三、Intent和PendingIntent的區別 ci

a. Intent是當即使用的,而PendingIntent能夠等到事件發生後觸發,PendingIntent能夠cancel
b. Intent在程序結束後即終止,而PendingIntent在程序結束後依然有效
c. PendingIntent自帶Context,而Intent須要在某個Context內運行
d. Intent在原task中運行,PendingIntent在新的task中運行 get

相關文章
相關標籤/搜索