为了实时获取通知栏状态的消息状态和内容进行获取,能够判断当前消息移除或者推送

  •     A service that receives calls from the system when new notifications are
  •     posted or removed, or their ranking changed.
  •     To extend this class, you must declare the service in your manifest file with
  •     the {@link android.Manifest.permission#BIND_NOTIFICATION_LISTENER_SERVICE} permission
  •     and include an intent filter with the {@link #SERVICE_INTERFACE} action. For example:

监听注册通知监听服务
 

                                                

判断监听通知栏使用权权限是否开启
 

// 判断是否开启监听通知权限    if (NotificationManagerCompat.getEnabledListenerPackages(this).contains(getPackageName())) {Intent serviceIntent =new Intent(this, GuardNotificationListenerService.class);        startService(serviceIntent);    }else {// 去开启 监听通知权限        startActivity(new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"));    }}

实现监听服务 NotificationListenerService
 

import android.app.Notification;import android.nfc.Tag;import android.os.Bundle;import android.service.notification.NotificationListenerService;import android.service.notification.StatusBarNotification;import android.text.SpannableString;import android.text.TextUtils;import com.mo.util.Log;/** * @Description: * @Author: lhw * @CreateDate: 2020/2/22 17:59 */public class GuardNotificationListenerService extends NotificationListenerService {    public final  static String TAG=GuardNotificationListenerService.class.getSimpleName();    @Override    public void onListenerConnected() {        super.onListenerConnected();        Log.d(TAG,"onListenerConnected");    }    @Override    public void onListenerDisconnected() {        super.onListenerDisconnected();        Log.d(TAG,"onListenerDisconnected");    }    @Override    public void onNotificationPosted(StatusBarNotification sbn) {        Log.d(TAG,"onNotificationPosted");        showMsg(sbn);    }    private void showMsg(StatusBarNotification sbn) {        Bundle extras = sbn.getNotification().extras;               String packageName = sbn.getPackageName();        if (extras!=null){        //获取通知消息标题            String title = extras.getString(Notification.EXTRA_TITLE);            // 获取通知消息内容            Object msgText = extras.getCharSequence(Notification.EXTRA_TEXT);//注意:获取的通知信息和短信的传递内容不一样 短信为SpannableString 这里容易造成转换异常            if (msgText instanceof SpannableString){                Log.d(TAG,"is SpannableString ...."+((SpannableString) msgText).subSequence(0,((SpannableString) msgText).length()));            }else{                Log.d(TAG,"showMsg packageName="+packageName+",title="+title+",msgText="+msgText);            }        }else{            Log.d(TAG,"is null ...."+packageName);        }    }    @Override    public void onNotificationRemoved(StatusBarNotification sbn) {        Log.d(TAG,"onNotificationRemoved");        showMsg(sbn);    }}

 

 

更多相关文章

  1. android短信的接收和发送处理
  2. ionic3 升级
  3. Android(安卓)Sqlite使用中注意事项
  4. Android(安卓)消息分发使用EventBus的实例详解
  5. android中关于notification的最新用法
  6. android关闭整个应用程序
  7. 动态权限
  8. Android(安卓)播放本地视频的动态权限问题
  9. Android(安卓)判断程序是否已安装 & 启动另一个程序

随机推荐

  1. android:launchMode="singleTask"
  2. 为Android内核添加新驱动,并添加到menucon
  3. 详解Android中的Activity生命周期
  4. 修改android挂载时的盘符名
  5. Android与J2ME区别之我见(1)
  6. Android(安卓)之 SystemService
  7. Android(安卓)SDK下载慢的解决办法
  8. 【Android】Android(安卓)中string-array
  9. android流式布局热门标签的实现
  10. android如何调用显示和隐藏系统默认的输