国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android Notification 使用方法詳解

2019-10-22 18:29:06
字體:
來源:轉載
供稿:網友

Android Notification 使用方法詳解

用TaskStackBuilder來獲取PendingIntent處理點擊跳轉到別的Activity,首先是用一般的PendingIntent來進行跳轉。

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來跳轉到NotificationShow,在運行效果上來看,首先發送了一條Notification到通知欄上,然后這時,我退出程序,即MainActivity已經不存在了,回到home主菜單,看到Notification仍然存在,當然,我們還沒有點擊或者cancel它,現在去點擊Notification,跳轉到NotificationShow界面,然后我們按下Back鍵,發現直接回到主界面了。現在大多數android應用都是在通知欄中如果有Notification通知的話,點擊它,然后會直接跳轉到對應的應用程序的某個界面,這時如果回退,即按下Back鍵,會返回到該應用程序的主界面,而不是系統的主界面。所以用上面這種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();關于這個方法,我們查一下官方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的屬性相關聯。

那么我們就在manifest文件中添加這個屬性

<activity   android:name="com.shulf.notificationtest.NotificationShow"   android:parentActivityName=".MainActivity" > </activity> 

讓它的parentActivity為MainActivity,也就是說在NotificationShow這個界面點擊回退時,會跳轉到MainActivity這個界面,而不是像上面一樣直接回到了程序的主菜單。運行一下,最后效果確實是這樣。

以上實用Android Notification的實例詳解,如有疑問請留言或者到本站社區交流討論,本站關于Android開發的文章還有很多,希望大家搜出查閱,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 九龙坡区| 赤城县| 汉川市| 大埔县| 双江| 古交市| 保定市| 浑源县| 夏河县| 蕉岭县| 志丹县| 云林县| 梓潼县| 静乐县| 萨嘎县| 石家庄市| 临安市| 南宁市| 临颍县| 佳木斯市| 江都市| 承德县| 中山市| 柳州市| 东源县| 巴彦淖尔市| 晋城| 阆中市| 盘山县| 东辽县| 福贡县| 泰兴市| 南华县| 张家口市| 富蕴县| 小金县| 外汇| 景宁| 马龙县| 都昌县| 乌兰县|