Android(安卓)NotificationListenerService监听获取通知栏通知和短信信息
16lz
2021-01-24
为了实时获取通知栏状态的消息状态和内容进行获取,能够判断当前消息移除或者推送
- 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); }}
更多相关文章
- android下实现程序不操作一段时间,执行另一个程序
- Android7.0调用系统相机和裁剪
- Android(安卓)简单的照相机程序
- android实现状态栏添加图标的函数实例
- android 发短信、打电话、发邮件
- permission :protectionLevel 默认权限
- Android(安卓)7.0 FileUriExposedException 的处理
- [Android(安卓)基础] -- ueventd.rc 处理硬件设备权限和 android
- webservice android 设置head头发送 获取