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 是可用的而且和通知管理器链接成功时回调。
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); } }
<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>
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>
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()便可
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); }