无法弄清楚为什么通知侦听器服务不起作用


public class ReadNotifications extends NotificationListenerService {
Context context;
@Override
public void onCreate() {
super.onCreate();
context = getApplicationContext();
}
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
for (StatusBarNotification sbm : ReadNotifications.this.getActiveNotifications()) {
String title = sbm.getNotification().extras.getString("android.title");
String text = sbm.getNotification().extras.getString("android.text");
String package_name = sbm.getPackageName();
Log.v("Notification title is:", title);
Log.v("Notification text is:", text);
Log.v("Package Name is:", package_name);
}
}
}
<service android:name=".ReadNotifications"
android:label="@string/app_name"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
</service>

以前我可以让NotificationListenerService工作,但现在我不明白为什么它不工作。我尝试过按原样运行它,尝试从主活动启动服务,从同一应用程序创建通知,从另一应用程序发送通知,更改模拟器,但没有发送日志。

您应该在android设置中授予特殊权限,您可以这样打开它们(需要API 22级(:

if (!NotificationManagerCompat.getEnabledListenerPackages(this).contains(getPackageName())) {
startActivity(new Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS));
}

最新更新