Android监听系统通知

1. API简介

Android在4.3的版本中(即API 18)加入了NotificationListenerService,根据SDK的描述(AndroidDeveloper)能够知道,当系统收到新的通知或者通知被删除时,会触发NotificationListenerService的回调方法。同时在Android 4.4 中新增了Notification.extras 字段,也就是说能够继承NotificationListenerService获取系统通知具体信息。android

而后通常会重写下面这三个方法:web

onNotificationPosted(StatusBarNotification sbn) :当有新通知到来时会回调;
onNotificationRemoved(StatusBarNotification sbn) :当有通知移除时会回调;
onListenerConnected() :当 NotificationListenerService 是可用的而且和通知管理器链接成功时回调。

2. 系统监听功能实现

2.1 新建NotificationMonitor类继承自NotificationListenerService,这是监听系统消息的核心服务类

public class NotificationMonitor extends NotificationListenerService {
        @Override
        public void onNotificationPosted(StatusBarNotification sbn) {
                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("NotificationMonitor", "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("NotificationMonitor", "Notification removed " + notificationTitle + " & " + notificationText);
        }
}

2.2 NotificationMonitor服务类须要在AndroidManifest.xml中注册

<service android:name=".NotificationMonitor"
          android:label="@string/service_name"
          android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
     <intent-filter>
         <action android:name="android.service.notification.NotificationListenerService" />
     </intent-filter>
 </service>

2.3 新建程序的主入口MainActivity类,并在onCreate方法中开启监听服务NotificationMonitor

Intent intent = new Intent(this, NotificationMonitor.class);
 startService(intent);

同时须要在AndroidManifest.xml中注册MainActivityapp

<activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

2.4 因为系统消息监听权限须要手动设置,这里添加一个代码,打开设置页面,方便开启系统消息监听权限

public void openNotificationListenSettings() {
    try {
        Intent intent;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP_MR1) {
            intent = new Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS);
        } else {
            intent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
        }
        startActivity(intent);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
在Mainactivity的onCreate方法中调用openNotificationListenSettings()便可

2.5 监听到系统消息之后能够经过发广播的方式与MainActivity通讯,将监听消息传递到前台页面,并显示在列表中

public class MonitorBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String title = intent.getStringExtra("notificationTitle");
        String text = intent.getStringExtra("notificationText");
    }
}

同时须要在AndroidManifest.xml中注册MonitorBroadcastReceiveride

<receiver android:name="com.example.receiver.MonitorBroadcastReceiver">
            <intent-filter>
                 <action android:name="com.example.BROADCAST"/>
            </intent-filter>
</receiver>

最后在NotificationMonitor中接收到系统消息的地方发送广播就好了svg

public void onReceive(String notificationTitle, String notificationText) {
		Intent intent = new Intent("com.example.BROADCAST");
		intent.putExtra("notificationTitle", notificationTitle);
		intent.putExtra("notificationText",notificationText);
		sendBroadcast(intent);
}