日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

[转]Android中pendingIntent的深入理解

發布時間:2023/12/9 Android 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [转]Android中pendingIntent的深入理解 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

轉自;here

pendingIntent字面意義:等待的,未決定的Intent。
要得到一個pendingIntent對象,使用方法類的靜態方法?getActivity(Context, int, Intent, int),getBroadcast(Context, int, Intent, int),getService(Context, int, Intent, int)? 分別對應著Intent的3個行為,跳轉到一個activity組件、打開一個廣播組件和打開一個服務組件。
參數有4個,比較重要的事第三個和第一個,其次是第四個和第二個。可以看到,要得到這個對象,必須傳入一個Intent作為參數,必須有context作為參數。
pendingIntent是一種特殊的Intent。主要的區別在于Intent的執行立刻的,而pendingIntent的執行不是立刻的。pendingIntent執行的操作實質上是參數傳進來的Intent的操作,但是使用pendingIntent的目的在于它所包含的Intent的操作的執行是需要滿足某些條件的。
主要的使用的地方和例子:通知Notificatio的發送,短消息SmsManager的發送和警報器AlarmManager的執行等等。

Android的狀態欄通知(Notification)

如果需要查看消息,可以拖動狀態欄到屏幕下方即可查看消息。

步驟:

1獲取通知管理器NotificationManager,它也是一個系統服務

2建立通知Notification notification = new Notification(icon, null, when);

3為新通知設置參數(比如聲音,震動,燈光閃爍)

4把新通知添加到通知管理器

發送消息的代碼如下:

//獲取通知管理器

NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)

int icon = android.R.drawable.stat_notify_chat;

long when = System.currentTimeMillis();//通知發生的時間為系統當前時間

//新建一個通知,指定其圖標和標題

Notification notification = new Notification(icon, null, when);//第一個參數為圖標,第二個參數為短暫提示標題,第三個為通知時間

notification.defaults = Notification.DEFAULT_SOUND;//發出默認聲音

notification.flags |= Notification.FLAG_AUTO_CANCEL;//點擊通知后自動清除通知

Intent openintent = new Intent(this, OtherActivity.class);

PendingIntent contentIntent = PendingIntent.getActivity(this, 0, openintent, 0);//當點擊消息時就會向系統發送openintent意圖

notification.setLatestEventInfo(this, “標題”, “我是內容", contentIntent);

mNotificationManager.notify(0, notification);//第一個參數為自定義的通知唯一標識

?

重點是setLatestEventInfo( )方法的最后一個參數!!!!它是一個PendingIntent!!!!!!!!!

這里使用到了PendingIntent(pend本意是待定,不確定的意思)

PendingIntent可以看作是對Intent的包裝。PendingIntent主要持有的信息是它所包裝的Intent和當前Application的Context。正由于PendingIntent中保存有當前Application的Context,使它賦予帶他程序一種執行的Intent的能力,就算在執行時當前Application已經不存在了,也能通過存在PendingIntent里的Context照樣執行Intent。

?

PendingIntent的一個很好的例子:

SmsManager的用于發送短信的方法:

sendTextMessage(destinationAddress, scAddress, text, sentIntent, deliveryIntent);

第一個參數:destinationAddress對方手機號碼

第二個參數:scAddress短信中心號碼一般設置為空

第三個參數:text短信內容

第四個參數:sentIntent判斷短信是否發送成功,如果你沒有SIM卡,或者網絡中斷,則可以通過這個itent來判斷。注意強調的是“發送”的動作是否成功。那么至于對于對方是否收到,另當別論

第五個參數:deliveryIntent當短信發送到收件人時,會收到這個deliveryIntent。即強調了“發送”后的結果

就是說是在"短信發送成功"和"對方收到此短信"才會激活?sentIntent和deliveryIntent這兩個Intent。這也相當于是延遲執行了Intent


上面兩個例子可以理解,PendingIntent就是一個可以在滿足一定條件下執行的Intent,它相比于Intent的優勢在于自己攜帶有Context對象,這樣他就不必依賴于某個activity才可以存在。

//

本文主要介紹PendingIntent的作用舉例以及和Intent的區別,本文中代碼見AndroidDemo@GoogleCode。

1、PendingIntent作用

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

PendingIntent一般作為參數傳給某個實例,在該實例完成某個操作后自動執行PendingIntent上的Action,也可以通過PendingIntent的send函數手動執行,并可以在send函數中設置OnFinished表示send成功后執行的動作。

2、PendingIntent舉例

a. 系統通知欄

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表示設置點擊該通知的事件

b. 短信系統舉例

短信系統舉例代碼 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() {@Overridepublic 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() {@Overridepublic 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將分別在短信發送成功和對方接受成功時被廣播

3、Intent和PendingIntent的區別

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

///

PendingIntent用于描述Intent及其最終的行為.?
? ?? ???你可以通過getActivity(Context context, int requestCode, Intent intent, int flags)系列方法從系統取得一個用于啟動一個Activity的PendingIntent對象,

? ?? ? 可以通過getService(Context context, int requestCode, Intent intent, int flags)方法從系統取得一個用于啟動一個Service的PendingIntent對象

? ?? ???可以通過getBroadcast(Context context, int requestCode, Intent intent, int flags)方法從系統取得一個用于向BroadcastReceiver的Intent廣播的PendingIntent對象

? ?? ?? ?返回的PendingIntent可以遞交給別的應用程序,然后繼續處理。這里的話你可以稍后才處理PendingIntent中描述的Intent及其最終行為。

? ?? ???當你把PendingIntent遞交給別的程序進行處理時,PendingIntent仍然擁有PendingIntent原程序所擁有的權限(with the same permissions and identity).當你從系統取得一個PendingIntent時,一定要非常小心才行。比如,通常,如果Intent目的地是你自己的component(Activity/Service/BroadcastReceiver)的話,你最好采用在Intent中顯示指定目的component名字的方式,以確保Intent最終能發到目的,否則Intent最后可能不知道發到哪里了。一個PendingIntent就是Android系統中的一個token(節點,這個應該是Linux或C\C++用語)的一個對象引用,它描述了一些將用于retrieve的數據(這里,這些數據描述了Intent及其最終的行為)。

? ?? ???這就意味著即使PendingIntent原進程結束了的話, PendingIntent本身仍然還存在,可在其他進程(PendingIntent被遞交到的其他程序)中繼續使用.如果我在從系統中提取一個PendingIntent的,而系統中有一個和你描述的PendingIntent對等的PendingInent, 那么系統會直接返回和該PendingIntent其實是同一token的PendingIntent,而不是一個新的token和PendingIntent。然而你在從提取PendingIntent時,通過FLAG_CANCEL_CURRENT參數,讓這個老PendingIntent的先cancel()掉,這樣得到的pendingInten和其token的就是新的了。

? ?? ? 通過FLAG_UPDATE_CURRENT參數的話,可以讓新的Intent會更新之前PendingIntent中的Intent對象數據,例如更新Intent中的Extras。另外,我們也可以在PendingIntent的原進程中調用PendingIntent的cancel ()把其從系統中移除掉

轉載于:https://www.cnblogs.com/wangziqiang/p/3927078.html

總結

以上是生活随笔為你收集整理的[转]Android中pendingIntent的深入理解的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。