Android Notification 使用方法詳解
Android Notification 使用方法詳解
用TaskStackBuilder來獲取PendingIntent處理點擊跳轉(zhuǎn)到別的Activity,首先是用一般的PendingIntent來進行跳轉(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,在運行效果上來看,首先發(fā)送了一條Notification到通知欄上,然后這時,我退出程序,即MainActivity已經(jīng)不存在了,回到home主菜單,看到Notification仍然存在,當然,我們還沒有點擊或者cancel它,現(xiàn)在去點擊Notification,跳轉(zhuǎn)到NotificationShow界面,然后我們按下Back鍵,發(fā)現(xiàn)直接回到主界面了?,F(xiàn)在大多數(shù)android應用都是在通知欄中如果有Notification通知的話,點擊它,然后會直接跳轉(zhuǎn)到對應的應用程序的某個界面,這時如果回退,即按下Back鍵,會返回到該應用程序的主界面,而不是系統(tǒng)的主界面。所以用上面這種PendingIntent的做法達不到目的。這里我們使用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)一個stackBuilder實例,接下來addParentStack();關(guān)于這個方法,我們查一下官方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. 這句話是說添加一個activity,與這個activity的manifest文件中的parentActivityName的屬性相關(guān)聯(lián)。
那么我們就在manifest文件中添加這個屬性
<activity android:name="com.shulf.notificationtest.NotificationShow" android:parentActivityName=".MainActivity" > </activity>
讓它的parentActivity為MainActivity,也就是說在NotificationShow這個界面點擊回退時,會跳轉(zhuǎn)到MainActivity這個界面,而不是像上面一樣直接回到了程序的主菜單。運行一下,最后效果確實是這樣。
以上實用Android Notification的實例詳解,如有疑問請留言或者到本站社區(qū)交流討論,本站關(guān)于Android開發(fā)的文章還有很多,希望大家搜出查閱,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
- Android編程使用Service實現(xiàn)Notification定時發(fā)送功能示例
- Android 通知使用權(quán)(NotificationListenerService)的使用
- Android Notification的多種用法總結(jié)
- Android Notification使用方法詳解
- Android 使用AlarmManager和NotificationManager來實現(xiàn)鬧鐘和通知欄
- android使用NotificationListenerService監(jiān)聽通知欄消息
- Android實現(xiàn)Service下載文件,Notification顯示下載進度的示例
- Android Notification使用方法總結(jié)
相關(guān)文章
Android 下的 QuickJS Binding 庫特性使用詳解
這篇文章主要介紹了Android 下的 QuickJS Binding 庫特性使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-09-09Android ListView數(shù)據(jù)綁定顯示的三種解決方法
本篇文章小編為大家介紹,Android ListView數(shù)據(jù)綁定顯示的三種解決方法。需要的朋友參考下2013-04-04Android開發(fā)之OpenGL繪制2D圖形的方法分析
這篇文章主要介紹了Android開發(fā)之OpenGL繪制2D圖形的方法,結(jié)合實例形式分析了Android使用OpenGL ES的圖形繪制組件實現(xiàn)2D圖形繪制的原理、步驟及相關(guān)代碼注意事項,需要的朋友可以參考下2017-09-09Android中用RxJava和ViewPager實現(xiàn)輪播圖
現(xiàn)在App中實現(xiàn)一個輪播圖已經(jīng)是很多產(chǎn)品的標配了,這篇文章給大家詳細介紹了如何利用RxJava和ViewPager實現(xiàn)輪播圖,有需要的朋友們可以參考借鑒,下面來一起看看吧。2016-09-09Fragment跳轉(zhuǎn)時傳遞參數(shù)及結(jié)果回傳的方法(推薦)
今天總結(jié)一下Fragment間的參數(shù)傳遞及結(jié)果返回的方法,非常不錯,具有參考借鑒價值,需要的朋友參考下2017-01-01