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

  • 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:

监听注册通知监听服务

    <service            android:name=".permission.service.GuardNotificationListenerService"            android:label="@string/accessibility_service_label"            android:enabled="true"            android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">            <intent-filter>                <action android:name="android.service.notification.NotificationListenerService" />            </intent-filter>        </service>

判断监听权限是否开启

    // 判断是否开启监听通知权限    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. Android7.0调用系统相机和裁剪
  3. Android(安卓)简单的照相机程序
  4. android实现状态栏添加图标的函数实例
  5. android 发短信、打电话、发邮件
  6. permission :protectionLevel 默认权限
  7. Android(安卓)7.0 FileUriExposedException 的处理
  8. [Android(安卓)基础] -- ueventd.rc 处理硬件设备权限和 android
  9. webservice android 设置head头发送 获取

随机推荐

  1. android异步加载图片
  2. Android的SensorEventListener例子(摇晃手
  3. 对话框android
  4. Android修改system只读权限:remount
  5. Android之文件存储
  6. android Fragment + FragmentStatePagerA
  7. android 自动更新apk版本
  8. Android图片圆角处理
  9. android涂鸦
  10. 《Android(安卓)基础(十二)》 TextInputLay