Android 監(jiān)聽Notification 被清除實(shí)例代碼
前言
一般非常駐的Notification是可以被用戶清除的,如果能監(jiān)聽被清除的事件就可以做一些事情,比如推送重新計(jì)數(shù)的問題。
正文
private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { if (intent == null || context == null) { return; } mNotificationManager.cancel(NOTIFICATION_ID_LIVE); String type = intent.getStringExtra(PUSH_TYPE); if (PUSH_TYPE_LINK.equals(type)) { //mNumLinkes = 0; } else if (PUSH_TYPE_LIVE.equals(type)) { //mNumLives = 0; } //這里可以重新計(jì)數(shù) } }; private void sendLiveNotification() { Intent intent = new Intent(NOTIFICATION_CLICK_ACTION); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this); String title = "Push測試"; mBuilder.setContentTitle(title); mBuilder.setTicker(title); mBuilder.setContentText("https://233.tv/over140"); mBuilder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)); mBuilder.setSmallIcon(R.drawable.ic_action_cast); mBuilder.setDefaults(Notification.DEFAULT_ALL); mBuilder.setWhen(System.currentTimeMillis()); mBuilder.setContentIntent(PendingIntent.getBroadcast(this, NOTIFICATION_ID_LIVE, intent, 0)); mBuilder.setDeleteIntent(PendingIntent.getBroadcast(this, NOTIFICATION_ID_LIVE, new Intent(NOTIFICATION_DELETED_ACTION).putExtra(PUSH_TYPE, PUSH_TYPE_LIVE), 0)); mNotificationManager.notify(NOTIFICATION_ID_LIVE, mBuilder.build()); }
代碼說明
1、最重要的是setDeleteIntent,這個(gè)在API Level 11(Android 3.0) 新增的
2、注意不要設(shè)置setAutoCancel為true,否則監(jiān)聽器接收不到
3、這里統(tǒng)一了點(diǎn)擊通知和消除通知的操作
4、注意廣播在推送前要注冊(cè)好
實(shí)際使用中發(fā)現(xiàn)還是有一點(diǎn)問題,比如Service被Kill掉了,通知欄點(diǎn)擊就會(huì)沒有反應(yīng)了,這塊還需要再考慮一下,比如把Broadcast在AndroidMainfest中監(jiān)聽。
以上就是對(duì)Android Notification 事件的資料整理,希望能幫助Android開發(fā)的朋友。
- Android Notification使用方法詳解
- Android中使用Notification實(shí)現(xiàn)狀態(tài)欄的通知
- Android 使用AlarmManager和NotificationManager來實(shí)現(xiàn)鬧鐘和通知欄
- android使用NotificationListenerService監(jiān)聽通知欄消息
- Android實(shí)現(xiàn)Service下載文件,Notification顯示下載進(jìn)度的示例
- Android種使用Notification實(shí)現(xiàn)通知管理以及自定義通知欄實(shí)例(示例四)
- Android Notification的多種用法總結(jié)
相關(guān)文章
Android實(shí)現(xiàn)手勢滑動(dòng)識(shí)別功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)手勢滑動(dòng)識(shí)別功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06Android平臺(tái)基于Pull方式對(duì)XML文件解析與寫入方法詳解
這篇文章主要介紹了Android平臺(tái)基于Pull方式對(duì)XML文件解析與寫入方法,結(jié)合實(shí)例形式分析了Android基于Pull方式對(duì)xml文件解析及寫入操作的步驟、原理與操作技巧,需要的朋友可以參考下2016-10-10Android下通過httpClient發(fā)送GET和POST請(qǐng)求的實(shí)例代碼
這篇文章介紹了Android下通過httpClient發(fā)送GET和POST請(qǐng)求的實(shí)例代碼,有需要的朋友可以參考一下2013-08-08Android GPS獲取當(dāng)前經(jīng)緯度坐標(biāo)
這篇文章主要為大家詳細(xì)介紹了Android GPS獲取當(dāng)前經(jīng)緯度坐標(biāo),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05Android如何判斷一個(gè)點(diǎn)在不在多邊形區(qū)域內(nèi)
這篇文章主要為大家詳細(xì)介紹了Android判斷一個(gè)點(diǎn)在不在多邊形區(qū)域內(nèi)的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05Android自定義實(shí)現(xiàn)BaseAdapter的優(yōu)化布局
這篇文章主要為大家詳細(xì)介紹了Android自定義實(shí)現(xiàn)BaseAdapter的優(yōu)化布局,感興趣的小伙伴們可以參考一下2016-08-08解決Android studio用真機(jī)調(diào)試時(shí)logcat一直輸出日志問題
這篇文章主要介紹了解決Android studio用真機(jī)調(diào)試時(shí)logcat一直輸出日志問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-04-04Android自定義View實(shí)現(xiàn)隨手勢滑動(dòng)控件
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)隨手勢滑動(dòng)的控件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02