Android--pendingIntent Intent
PendingIntent
pendingIntent字面意義:等待的,未決定的Intent。
要得到一個pendingIntent對象,使用方法類的靜態(tài)方法?getActivity(Context, int, Intent, int),getBroadcast(Context, int, Intent, int),?getService(Context, int, Intent, int)? 分別對應(yīng)著Intent的3個行為,跳轉(zhuǎn)到一個activity組件、打開一個廣播組件和打開一個服務(wù)組件。
參數(shù)有4個,比較重要的事第三個和第一個,其次是第四個和第二個。可以看到,要得到這個對象,必須傳入一個Intent作為參數(shù),必須有context作為參數(shù)。
pendingIntent是一種特殊的Intent。主要的區(qū)別在于Intent的執(zhí)行立刻的,而pendingIntent的執(zhí)行不是立刻的。pendingIntent執(zhí)行的操作實質(zhì)上是參數(shù)傳進來的Intent的操作,但是使用pendingIntent的目的在于它所包含的Intent的操作的執(zhí)行是需要滿足某些條件的。
主要的使用的地方和例子:通知Notificatio的發(fā)送,短消息SmsManager的發(fā)送和警報器AlarmManager的執(zhí)行等等。
Android的狀態(tài)欄通知(Notification)
如果需要查看消息,可以拖動狀態(tài)欄到屏幕下方即可查看消息。
步驟:
1?獲取通知管理器NotificationManager,它也是一個系統(tǒng)服務(wù)
2?建立通知Notification notification = new Notification(icon, null, when);【注】:這種方法是舊版的API試用方法,如果是新版的需要試用Notifiication.Builder
3?為新通知設(shè)置參數(shù)(比如聲音,震動,燈光閃爍)
4?把新通知添加到通知管理器
發(fā)送消息的代碼如下:
//獲取通知管理器
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)
int icon = android.R.drawable.stat_notify_chat;
long when = System.currentTimeMillis();//通知發(fā)生的時間為系統(tǒng)當前時間
//新建一個通知,指定其圖標和標題
Notification notification = new Notification(icon, null, when);//第一個參數(shù)為圖標,第二個參數(shù)為短暫提示標題,第三個為通知時間
notification.defaults = Notification.DEFAULT_SOUND;//發(fā)出默認聲音
notification.flags |= Notification.FLAG_AUTO_CANCEL;//點擊通知后自動清除通知
Intent openintent = new Intent(this, OtherActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, openintent, 0);//當點擊消息時就會向系統(tǒng)發(fā)送openintent意圖
notification.setLatestEventInfo(this, “標題”, “我是內(nèi)容", contentIntent);
mNotificationManager.notify(0, notification);//第一個參數(shù)為自定義的通知唯一標識
?
重點是setLatestEventInfo( )方法的最后一個參數(shù)!!!!它是一個PendingIntent!!!!!!!!!
這里使用到了PendingIntent(pend本意是待定,不確定的意思)
PendingIntent可以看作是對Intent的包裝。PendingIntent主要持有的信息是它所包裝的Intent和當前Application的Context。正由于PendingIntent中保存有當前Application的Context,使它賦予帶他程序一種執(zhí)行的Intent的能力,就算在執(zhí)行時當前Application已經(jīng)不存在了,也能通過存在PendingIntent里的Context照樣執(zhí)行Intent。
?
PendingIntent的一個很好的例子:
SmsManager的用于發(fā)送短信的方法:
sendTextMessage(destinationAddress, scAddress, text, sentIntent, deliveryIntent);
第一個參數(shù):destinationAddress?對方手機號碼
第二個參數(shù):scAddress?短信中心號碼?一般設(shè)置為空
第三個參數(shù):text?短信內(nèi)容
第四個參數(shù):sentIntent判斷短信是否發(fā)送成功,如果你沒有SIM卡,或者網(wǎng)絡(luò)中斷,則可以通過這個itent來判斷。注意強調(diào)的是“發(fā)送”的動作是否成功。那么至于對于對方是否收到,另當別論
第五個參數(shù):deliveryIntent當短信發(fā)送到收件人時,會收到這個deliveryIntent。即強調(diào)了“發(fā)送”后的結(jié)果
就是說是在"短信發(fā)送成功"和"對方收到此短信"才會激活?sentIntent和deliveryIntent這兩個Intent。這也相當于是延遲執(zhí)行了Intent
上面兩個例子可以理解,PendingIntent就是一個可以在滿足一定條件下執(zhí)行的Intent,它相比于Intent的優(yōu)勢在于自己攜帶有Context對象,這樣他就不必依賴于某個activity才可以存在。
PendingIntent和Intent的區(qū)別: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英文意思是意圖,pending表示即將發(fā)生或來臨的事情。?
PendingIntent這個類用于處理即將發(fā)生的事情。比如在通知Notification中用于跳轉(zhuǎn)頁面,但不是馬上跳轉(zhuǎn)。?
Intent 是及時啟動,intent 隨所在的activity 消失而消失。?
PendingIntent 可以看作是對intent的包裝,通常通過getActivity,getBroadcast ,getService來得到pendingintent的實例,當前activity并不能馬上啟動它所包含的intent,而是在外部執(zhí)行 pendingintent時,調(diào)用intent的。正由于pendingintent中 保存有當前App的Context,使它賦予外部App一種能力,使得外部App可以如同當前App一樣的執(zhí)行pendingintent里的 Intent, 就算在執(zhí)行時當前App已經(jīng)不存在了,也能通過存在pendingintent里的Context照樣執(zhí)行Intent(就像在手機上關(guān)掉了信息,照樣可以通過通知欄的短信通知,來打開通知欄上顯示的信息)。另外還可以處理intent執(zhí)行后的操作。常和alermanger 和notificationmanager一起使用。?
Intent一般是用作Activity、Sercvice、BroadcastReceiver之間傳遞數(shù)據(jù),而Pendingintent,一般用在 Notification上,可以理解為延遲執(zhí)行的intent,PendingIntent是對Intent一個包裝。
?
【參閱】http://blog.csdn.net/zeng622peng/article/details/6180190
轉(zhuǎn)載于:https://www.cnblogs.com/plxx/p/3939769.html
總結(jié)
以上是生活随笔為你收集整理的Android--pendingIntent Intent的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: OncePerRequestFilter
- 下一篇: 【POJ】2065 SETI