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:

監聽註冊通知監聽服務
 

<service
            android:name=".permission.service.GuardNotificationListenerService"
            android:label="@string/app_name"
            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);
    }


}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章