答案 0 :(得分:2)
它适用于:Android 4.3(JELLY_BEAN_MR2)。
添加到您的清单新权限android.permission.BIND_NOTIFICATION_LISTENER_SERVICE
。
创建NotificationListenerService类并添加到清单中 来自Google Developer:
NotificationListenerService必须要求,以确保这一点 只有系统可以绑定它。
的AndroidManifest.xml:
<service android:name=".NotificationListener"
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>
onNotificationPosted()
。 NotificationListenerService类:
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
public class NotificationListenerService extends NotificationListenerService {
@Override
public void onNotificationPosted(final StatusBarNotification sbn) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
// Here you need to play with the sbn object and get the data that you want from it.
//sbn.getPackageName()
Notification notification = sbn.getNotification();
Bundle extras = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
extras = notification.extras;
}
if (extras != null) {
extras.get("android.title");
}
}
}
@Override
public void onNotificationRemoved(StatusBarNotification sbn) {
}
@Override
@TargetApi(Build.VERSION_CODES.N)
public void onListenerDisconnected() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
// Notification listener disconnected - requesting rebind
requestRebind(new ComponentName(this, NotificationListenerService2.class));
}
}
}
实施的简单示例应用程序:https://github.com/kpbird/NotificationListenerService-Example/