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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

PendingIntent与Intent区别

發布時間:2025/3/20 编程问答 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 PendingIntent与Intent区别 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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); }

?

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();

代碼解釋
????? 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廣播。

如果你的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等都會用到。。。
????? 查看PendingIntent 類可以看到許多的Send函數,就是PendingIntent在進行被賦予的相關的操作。

總結

以上是生活随笔為你收集整理的PendingIntent与Intent区别的全部內容,希望文章能夠幫你解決所遇到的問題。

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

主站蜘蛛池模板: 美女又爽又黄免费 | 亚洲自拍中文 | 91免费视频 | 午夜中出| 精品动漫一区二区三区 | 一区久久 | 中文字幕高清在线 | 亚洲AV无码精品一区二区三区 | av av在线 | 91国偷自产一区二区三区观看 | 放几个免费的毛片出来看 | 成年人国产 | 久久久永久久久人妻精品麻豆 | 日韩高清二区 | 国产精品对白 | 国产精品腿扒开做爽爽爽挤奶网站 | 天天爽天天爽夜夜爽毛片 | 中文字幕精品三区 | 一级特黄aa大片欧美 | 毛片av在线| 91抖音在线观看 | 日韩性网站 | 九九热re | 色综合久久88色综合天天免费 | 成人视屏在线 | 亚洲精品福利在线 | 国产精品网址 | www精品视频 | 欧美成人免费在线视频 | 亚洲v国产 | 少妇诱惑av | 男女免费视频 | 日韩av片在线看 | 亚洲国产日韩欧美在线观看 | 狠狠看 | 叶全真三级 | 国产精品高潮呻吟久久av黑人 | 91免费高清在线观看 | 欣赏asian国模裸体pics | 日韩精品视频免费在线观看 | 日色视频 | www日韩av| 久久亚洲AV无码 | 天天综合色网 | 玩偶姐姐在线看 | 精品黑人一区二区三区在线观看 | 成人性生交大片免费看中文 | 亚洲av高清一区二区三区 | 欧美区国产区 | 国产精品久久福利 | 污污网站在线观看 | 丰满人妻一区二区三区四区53 | 国产一区二区三区在线视频观看 | 成人无码精品1区2区3区免费看 | 国产精品久久中文字幕 | 天堂影视在线观看 | 欧美激情专区 | 亚洲综合成人网 | 你懂的网址在线观看 | 日一日射一射 | 天天综合色网 | 96精品在线| 在线观看国产 | 91成人免费看 | 天天干网址| 一区二区三区精品国产 | 国产福利在线观看视频 | 成人动漫一区二区 | 国产精品视频专区 | 亚洲精品视频在线看 | 日韩有码中文字幕在线观看 | 91午夜精品 | 精品在线观看一区二区 | 亚洲日本中文字幕在线 | www午夜视频 | 好男人在线观看 | 国产久精品 | 日韩av一区二区在线播放 | 久久久18| 亚洲啊啊 | 日本a在线 | 色视频网站 | 成年人视频网 | 奶水旺盛的少妇在线播放 | 大又大又粗又硬又爽少妇毛片 | 国产欧美日韩三区 | 蜜桃视频无码区在线观看 | 亚洲一区在线免费观看 | 亚洲天堂资源在线 | 在线免费观看日韩视频 | 蜜桃臀av在线 | 亚洲国产中文字幕在线观看 | 91视频国产一区 | 青青草精品在线 | 久久久五月天 | 新婚之夜玷污岳丰满少妇在线观看 | 国产乡下妇女做爰毛片 | 亚洲黄色网址大全 | 成人免费做受小说 |