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

首頁 > 系統 > Android > 正文

Android Notification 使用方法詳解

2019-12-12 02:09:24
字體:
來源:轉載
供稿:網友

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開發的文章還有很多,希望大家搜出查閱,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 馆陶县| 陆良县| 温宿县| 宜阳县| 新密市| 大厂| 自治县| 大渡口区| 肃宁县| 上蔡县| 洛扎县| 海原县| 江川县| 军事| 肥乡县| 维西| 吐鲁番市| 山西省| 乌恰县| 聂拉木县| 定襄县| 新巴尔虎右旗| 当雄县| 九江市| 务川| 黄冈市| 家居| 南乐县| 扶风县| 苏尼特左旗| 平舆县| 井冈山市| 永兴县| 虞城县| 贵德县| 安塞县| 赞皇县| 宁海县| 防城港市| 南涧| 汉中市|