Android新优化

Android爬取第三方app推送消息,获取应用通知信息

2019-07-24  本文已影响0人  余炳高笔记

来不及解释,直接上效果图

抓去状态栏消息 Activity NotificationMonitorService  AndroidManifest.xml 

不想敲代码就复制下方代码

Androdi代码

自定义 NotificationMonitorService  继承 NotificationListenerService 

import android.app.Notification;

import android.os.Bundle;

import android.service.notification.NotificationListenerService;

import android.service.notification.StatusBarNotification;

import android.util.Log;

public class NotificationMonitorService  extends NotificationListenerService {

// 在收到消息时触发

    @Override

    public void onNotificationPosted(StatusBarNotification sbn) {

// TODO Auto-generated method stub

        Bundle extras = sbn.getNotification().extras;

        // 获取接收消息APP的包名

        String notificationPkg = sbn.getPackageName();

        // 获取接收消息的抬头

        String notificationTitle = extras.getString(Notification.EXTRA_TITLE);

        // 获取接收消息的内容

        String notificationText = extras.getString(Notification.EXTRA_TEXT);

        Log.i("NotificationInfo", " Notification posted " + notificationTitle +" & " + notificationText);

    }

// 在删除消息时触发

    @Override

    public void onNotificationRemoved(StatusBarNotification sbn) {

// TODO Auto-generated method stub

        Bundle extras = sbn.getNotification().extras;

        // 获取接收消息APP的包名

        String notificationPkg = sbn.getPackageName();

        // 获取接收消息的抬头

        String notificationTitle = extras.getString(Notification.EXTRA_TITLE);

        // 获取接收消息的内容

        String notificationText = extras.getString(Notification.EXTRA_TEXT);

        Log.i("NotificationInfo", " Notification removed " + notificationTitle +" & " + notificationText);

    }

}

注册 AndroidManifest.xml

<service  android:name=".ui.NotificationMonitorService"

                android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">

        <intent-filter>

            <action android:name="android.service.notification.NotificationListenerService" />

        </intent-filter>

</service>

Activity 代码

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

    // 判断是否开启监听通知权限

    if (NotificationManagerCompat.getEnabledListenerPackages(this).contains(getPackageName())) {

Intent serviceIntent =new Intent(this, NotificationMonitorService.class);

        startService(serviceIntent);

    }else {

// 去开启 监听通知权限

        startActivity(new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"));

    }

}

如果应用在后台被干掉  可以通过第三推送唤醒应用

上一篇下一篇

猜你喜欢

热点阅读