欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android Notification 使用方法詳解

 更新時間:2017年08月29日 09:00:52   投稿:lqh  
這篇文章主要介紹了Android Notification 使用方法詳解的相關(guān)資料,這里提供實例來幫助大家理解掌握這部分內(nèi)容,需要的朋友可以參考下

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

相關(guān)文章

最新評論