Android利用NotificationListenerService监听获取通知栏使用权,统计微信加好友功能

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

  •     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);
    }


}

 

 

你可能感兴趣的:(android,面试)