標籤:des android style blog http io color ar os
轉載: PendingIntent
2010-11-16 00:03:55
標籤:PendingIntent Intent SmsManager getBroadcast BroadcastReveiver
原創作品,允許轉載,轉載時請務必以超連結形式標明文章 原始出處 、作者資訊和本聲明。否則將追究法律責任。http://lbrant.blog.51cto.com/2400264/424154
Intent和PendingIntent的關係,初學的時候很迷惑,用PendingIntent的時候,還會出現奇怪的問題,比如無法傳遞資料,無法更新資料,所以我集眾家之長,加上我個人的一些實踐,總結如下,希望能給你一些協助。
首先看官方解釋:An Intent is something that is used right now; a PendingIntent is something that may create an Intent in the future. You will use a PendingIntent with Notifications, AlarmManager, etc.
Intent大家都很熟悉了,就是一個意圖,這個意圖表明要啟動哪個Activity,Service,PendingIntent可以看作是對Intent的進一步封裝,它是對Intent的描述,我們可以把這個描述交給別的程式,別的程式根據這個描述在後面的時間做你安排做的事情,下面是一個發送SMS簡訊的例子:
String msg ="你好"; String number = "135****6784"; SmsManager sms = SmsManager.getDefault(); PendingIntent pi = PendingIntent.getBroadcast(SmsActivity.this,0,new Intent(XXX),0); sms.sendTextMessage(number, null, msg, pi, null); Toast.makeText(SmsActivity.this,"發送成功",Toast.LENGHT_LONG).show();
方法SmsManager.sendTextMessage(String destinationAddress, String scAddress, String text, PendingIntent sentIntent, PendingIntent deliveryIntent):
PendingIntent sentIntent:當簡訊發出時,成功的話sendIntent會把其內部的描述的intent廣播出去,當然失敗之後會產生錯誤碼,並通過 android.app.PendingIntent.OnFinished進行回調("Callback");
PendingIntent deliveryIntent:是當訊息已經傳遞給收信人後所進行的Intent廣播;
如果你的BroadcastReveiver註冊接收相應的訊息,你就會收到相應的Intent,這時候就可以根據Intent的Action,執行相應的動作,這就是上面說的in the future的含義;
有三個靜態方法可以獲得PendingIntent執行個體:
public static PendingIntent getBroadcast(Context context, int requestCode, Intent intent, int flags)
public static PendingIntent getActivity(Context context, int requestCode, Intent intent, int flags)
public static PendingIntent getService(Context context, int requestCode, Intent intent, int flags)
flags參數有三個,我覺得英文更好理解:
FLAG_ONE_SHOT:this PendingIntent can only be used once. If set, after send() is called on it, it will be automatically canceled for you and any future attempt to send through it will fail.
FLAG_UPDATE_CURRENT: if the described PendingIntent already exists, then keep it but its replace its extra data with what is in this new Intent. This can be used if you are creating intents where only the extras change, and don‘t care that any entities that received your previous PendingIntent will be able to launch it with your new extras even if they are not explicitly given to it.
這個簡單解釋一下,就是當存在時,先把原來的取消,然後建立一個新的,在AlarmManager服務時,修改一個鬧鈴,用的比較笨的的方法,先取消,然後重新註冊,其實加上這個參數就行了。
要注意的是,這個只更新extra data,不會修改其他內容,不能new一個Intent,還有就是如果你的Intent中需要傳遞Id或者其他資料,一定要用這個flags或者FLAG_CANCEL_CURRENT,曾經一直接收不到Id,查了半天原來是這個原因 :-(
LAG_NO_CREATE:if the described PendingIntent does not already exist, then simply return null instead of creating it.
LAG_CANCEL_CURRENT:if the described PendingIntent already exists, the current one is canceled before generating a new one.You can use this to retrieve a new PendingIntent when you are only changing the extra data in the Intent; by canceling the previous pending intent, this ensures that only entities given the new data will be able to launch it. If this assurance is not an issue, consider FLAG_UPDATE_CURRENT.
上面4個flag中最經常使用的是FLAG_UPDATE_CURRENT,因為描述的Intent有更新的時候需要用到這個flag去更新你的描述(確切的說是更新extra data),否則組件在下次事件發生或時間到達的時候extras永遠是第一次Intent的extras,使用FLAG_CANCEL_CURRENT也能做到更新extras,只不過是先把前面的extras清除,另外FLAG_CANCEL_CURRENT和FLAG_UPDATE_CURRENT的區別在於能否新new一個Intent,FLAG_CANCEL_CURRENT能夠新new一個Intent,而FLAG_UPDATE_CURRENT則不能,只能使用第一次的Intent。
還有一個問題就是怎麼區分PendingIntent,主要取消的時候要用到,requestCode可以區分,但系統還是根據Intent的Action去區分的,如果Intent設定了Class,classData,取消的時候Intent一定要設定要相同的,不然取消不掉就可能出現取消後,Alarm還會響的問題,PendingIntent用的地方蠻多的,像 Notifications, AlarmManager等都會用到。。。
以下是參考的文章:
http://yichen914.spaces.live.com/blog/cns!723590D920FAF62B!703.entry
http://hi.chinaunix.net/?uid-21747227-action-viewspace-itemid-48368
http://jimmymouse.javaeye.com/blog/760505
本文出自 “超越夢想” 部落格,請務必保留此出處http://lbrant.blog.51cto.com/2400264/424154
------------------------------------------------------------------------------------------------------------------------------------------
pendingIntent簡單理解
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才可以存在。
關於Android中PendingIntent的認識