Android Notification 使用方法詳解
Android Notification 使用方法詳解
用TaskStackBuilder來獲取PendingIntent處理點(diǎn)擊跳轉(zhuǎn)到別的Activity,首先是用一般的PendingIntent來進(jìn)行跳轉(zhuǎn)。
mBuilder = new NotificationCompat.Builder(this).setContent(view) .setSmallIcon(R.drawable.icon).setTicker("新資訊") .setWhen(System.currentTimeMillis()) .setOngoing(false) .setAutoCancel(true); Intent intent = new Intent(this, NotificationShow.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); mBuilder.setContentIntent(pendingIntent);
直接用PendingIntent來跳轉(zhuǎn)到NotificationShow,在運(yùn)行效果上來看,首先發(fā)送了一條Notification到通知欄上,然后這時(shí),我退出程序,即MainActivity已經(jīng)不存在了,回到home主菜單,看到Notification仍然存在,當(dāng)然,我們還沒有點(diǎn)擊或者cancel它,現(xiàn)在去點(diǎn)擊Notification,跳轉(zhuǎn)到NotificationShow界面,然后我們按下Back鍵,發(fā)現(xiàn)直接回到主界面了?,F(xiàn)在大多數(shù)android應(yīng)用都是在通知欄中如果有Notification通知的話,點(diǎn)擊它,然后會(huì)直接跳轉(zhuǎn)到對(duì)應(yīng)的應(yīng)用程序的某個(gè)界面,這時(shí)如果回退,即按下Back鍵,會(huì)返回到該應(yīng)用程序的主界面,而不是系統(tǒng)的主界面。所以用上面這種PendingIntent的做法達(dá)不到目的。這里我們使用TaskStackBuilder來做。
mBuilder = new NotificationCompat.Builder(this).setContent(view) .setSmallIcon(R.drawable.icon).setTicker("新資訊") .setWhen(System.currentTimeMillis()) .setOngoing(false) .setAutoCancel(true); Intent intent = new Intent(this, NotificationShow.class); TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); stackBuilder.addParentStack(NotificationShow.class); stackBuilder.addNextIntent(intent); PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); // PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, // intent, PendingIntent.FLAG_UPDATE_CURRENT); mBuilder.setContentIntent(pendingIntent);
顯示用TaskStackBuilder.create(this)一個(gè)stackBuilder實(shí)例,接下來addParentStack();關(guān)于這個(gè)方法,我們查一下官方API文檔:Add the activity parent chain as specified by the parentActivityName attribute of the activity (or activity-alias) element in the application's manifest to the task stack builder. 這句話是說添加一個(gè)activity,與這個(gè)activity的manifest文件中的parentActivityName的屬性相關(guān)聯(lián)。
那么我們就在manifest文件中添加這個(gè)屬性
<activity android:name="com.shulf.notificationtest.NotificationShow" android:parentActivityName=".MainActivity" > </activity>
讓它的parentActivity為MainActivity,也就是說在NotificationShow這個(gè)界面點(diǎn)擊回退時(shí),會(huì)跳轉(zhuǎn)到MainActivity這個(gè)界面,而不是像上面一樣直接回到了程序的主菜單。運(yùn)行一下,最后效果確實(shí)是這樣。
以上實(shí)用Android Notification的實(shí)例詳解,如有疑問請(qǐng)留言或者到本站社區(qū)交流討論,本站關(guān)于Android開發(fā)的文章還有很多,希望大家搜出查閱,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
- Android編程使用Service實(shí)現(xiàn)Notification定時(shí)發(fā)送功能示例
- Android 通知使用權(quán)(NotificationListenerService)的使用
- Android Notification的多種用法總結(jié)
- Android Notification使用方法詳解
- Android 使用AlarmManager和NotificationManager來實(shí)現(xiàn)鬧鐘和通知欄
- android使用NotificationListenerService監(jiān)聽通知欄消息
- Android實(shí)現(xiàn)Service下載文件,Notification顯示下載進(jìn)度的示例
- Android Notification使用方法總結(jié)
相關(guān)文章
Android 下的 QuickJS Binding 庫特性使用詳解
這篇文章主要介紹了Android 下的 QuickJS Binding 庫特性使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09Android Studio配置反混淆的實(shí)現(xiàn)
這篇文章主要介紹了Android Studio如何混淆的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10Android ListView數(shù)據(jù)綁定顯示的三種解決方法
本篇文章小編為大家介紹,Android ListView數(shù)據(jù)綁定顯示的三種解決方法。需要的朋友參考下2013-04-04Android開發(fā)之OpenGL繪制2D圖形的方法分析
這篇文章主要介紹了Android開發(fā)之OpenGL繪制2D圖形的方法,結(jié)合實(shí)例形式分析了Android使用OpenGL ES的圖形繪制組件實(shí)現(xiàn)2D圖形繪制的原理、步驟及相關(guān)代碼注意事項(xiàng),需要的朋友可以參考下2017-09-09Android ROM升級(jí)包的選項(xiàng)解釋
本文主要解釋 Android刷機(jī)過程中會(huì)遇到apply sdcard:update.zip(刷ROM升級(jí)包)的選項(xiàng),對(duì)選項(xiàng)解釋,以免造成沒必要的麻煩,希望對(duì)大家有用!2016-07-07Android中用RxJava和ViewPager實(shí)現(xiàn)輪播圖
現(xiàn)在App中實(shí)現(xiàn)一個(gè)輪播圖已經(jīng)是很多產(chǎn)品的標(biāo)配了,這篇文章給大家詳細(xì)介紹了如何利用RxJava和ViewPager實(shí)現(xiàn)輪播圖,有需要的朋友們可以參考借鑒,下面來一起看看吧。2016-09-09Fragment跳轉(zhuǎn)時(shí)傳遞參數(shù)及結(jié)果回傳的方法(推薦)
今天總結(jié)一下Fragment間的參數(shù)傳遞及結(jié)果返回的方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下2017-01-01