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

Android 監(jiān)聽Notification 被清除實(shí)例代碼

 更新時(shí)間:2016年07月21日 09:25:46   投稿:lqh  
本文主要介紹Android 監(jiān)聽Notification 事件,這里給大家提供實(shí)例代碼進(jìn)行參考,有需要的小伙伴可以參考下

前言

 一般非常駐的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ā)的朋友。

相關(guān)文章

最新評(píng)論