0. Learning articles
Refer to the following Blog without any extra code
https://blog.csdn.net/wanghang1208/article/details/49905403
The original Baidu guard notice bar is similar to this principle, very good
1. Demonstration results
Data source
log monitored
03-24 14:37:04.264 6155-6155/com.lava.noticeobser D/MyNotificationListenService: onNotificationRemoved 03-24 14:39:19.928 6155-6155/com.lava.noticeobser D/MyNotificationListenService: onNotificationPosted 03-24 14:39:19.928 6155-6155/com.lava.noticeobser I/MyNotificationListenService: notify msg: title = the temperature in many places in the North has reached a new high, and a new round of rainfall in the south is coming. Details →, when = 1521873559186, contenttitle = obvious warming range! , contentText = the temperature in many places in the North has reached a new high, and a new round of rainfall is coming from the south. Details →, contentText= 03-24 14:39:20.235 6155-6155/com.lava.noticeobser D/MyNotificationListenService: onNotificationPosted 03-24 14:39:20.235 6155-6155/com.lava.noticeobser I/MyNotificationListenService: notify msg: title = please eat these delicacies in the spring anyway →, when = 1521873560030, contenttitle = miss one year! , contentText = please eat these delicacies in the spring anyway →, contentSubtext= 03-24 14:39:54.276 6155-6155/com.lava.noticeobser D/MyNotificationListenService: onNotificationPosted 03-24 14:39:54.276 6155-6155/com.lava.noticeobser I/MyNotificationListenService: notify msg: title = saving screenshot..., when = 1521873594184, contenttitle = saving screenshot..., contentText = saving screenshot. ,contentSubtext= 03-24 14:39:55.156 6155-6155/com.lava.noticeobser D/MyNotificationListenService: onNotificationPosted 03-24 14:39:55.156 6155-6155/com.lava.noticeobser I/MyNotificationListenService: notify msg: title = saving screenshot..., when = 1521873595096, contenttitle = screenshot captured. , contentText = Click to view your screenshot. ,contentSubtext=
2. source code
- MyNotificationListenService is responsible for listening to messages
package com.lava.noticeobser;
import android.app.Notification;
import android.os.Build;
import android.os.Bundle;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.util.Log;
import java.lang.reflect.Field;
public class MyNotificationListenService extends NotificationListenerService {
private static final String TAG = MyNotificationListenService.class.getSimpleName();
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
super.onNotificationPosted(sbn);
Log.d(TAG, "onNotificationPosted");
Notification n = sbn.getNotification();
if (n == null) {
return;
}
// Title and time
String title = "";
if (n.tickerText != null) {
title = n.tickerText.toString();
}
long when = n.when;
// Other information exists in a bundle, which is private before Android 4.3 and needs to be obtained through reflection; it can be obtained directly after Android 4.3
Bundle bundle = null;
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.JELLY_BEAN_MR2) {
// android 4.3
try {
Field field = Notification.class.getDeclaredField("extras");
bundle = (Bundle) field.get(n);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
} else if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR2) {
// After android 4.3
bundle = n.extras;
}
// Content title, content and sub content
String contentTitle = bundle.getString(Notification.EXTRA_TITLE);
if (contentTitle == null) {
contentTitle = "";
}
String contentText = bundle.getString(Notification.EXTRA_TEXT);
if (contentText == null) {
contentText = "";
}
String contentSubtext = bundle.getString(Notification.EXTRA_SUB_TEXT);
if (contentSubtext == null) {
contentSubtext = "";
}
Log.i(TAG, "notify msg: title=" + title + " ,when=" + when
+ " ,contentTitle=" + contentTitle + " ,contentText="
+ contentText + " ,contentSubtext=" + contentSubtext);
}
@Override
public void onNotificationRemoved(StatusBarNotification sbn) {
super.onNotificationRemoved(sbn);
Log.d(TAG, "onNotificationRemoved");
}
}
- Mainfest.xml to assist MyNotificationListenService
<service
android:name=".MyNotificationListenService"
android:label="mynotifyservice"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE" >
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
</service>
- Opening service
package com.lava.noticeobser;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startNotificationListenService();
}
// Sending notice
public void sendNotice(View view) {
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification n;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
// android 3.0before
n = new Notification(R.mipmap.ic_launcher, "title",
System.currentTimeMillis());
} else {
// android 3.0after
n = new Notification.Builder(MainActivity.this)
.setSmallIcon(R.mipmap.ic_launcher).setTicker("title")
.setContentTitle("content title")
.setContentText("content text").setSubText("sub text")
.setWhen(System.currentTimeMillis()).build();
}
manager.notify(0, n);
}
// Jump to the authority interface of service notification
public void settingsNotice(View view) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
Intent intent = new Intent(
"android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
startActivity(intent);
} else {
Toast.makeText(MainActivity.this, "This function is not supported by the system of the phone", Toast.LENGTH_SHORT)
.show();
}
}
// Start listening message notification service
private void startNotificationListenService() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
Intent intent = new Intent(MainActivity.this,
MyNotificationListenService.class);
startService(intent);
} else {
Toast.makeText(MainActivity.this, "This function is not supported by the system of the phone", Toast.LENGTH_SHORT)
.show();
}
}
}
- Simple and rough layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.lava.noticeobser.MainActivity"
android:orientation="vertical">
<Button
android:onClick="sendNotice"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Send notice" />
<Button
android:onClick="settingsNotice"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Enable listening permission" />
</LinearLayout>