我已经研究了很多关于这一点,但我无法找到一个单一的工作解决方案。 我希望我的应用程序禁用所有通知,不是通过一个按钮或什么,但自动。 我正在开发一个应用程序,有一个无声的屏幕,即屏幕是黑色的,但通知是吓坏了我。 我已经尝试了NotificationListener,这样无论何时它被发布但对我不起作用,它都会自动取消它们。 有天才能解决我的问题吗?
public class NotificationListenerEx extends NotificationListenerService {
public BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
NotificationListenerEx.this.cancelAllNotifications();
}
};
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
super.onNotificationPosted(sbn);
}
@Override
public void onNotificationRemoved(StatusBarNotification sbn) {
super.onNotificationRemoved(sbn);
}
@Override
public IBinder onBind(Intent intent) {
Log.i("Msg", "Notification Removed");
return super.onBind(intent);
}
@Override
public void onDestroy() {
unregisterReceiver(broadcastReceiver);
super.onDestroy();
}
@Override
public void onCreate() {
super.onCreate();
registerReceiver(broadcastReceiver, new IntentFilter("com.test.app"));
}
}
清单文件中的服务
<service android:name=".NotificationListenerEx"
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>
然后,为了自动触发它,我将它放入onStart()
getApplicationContext().sendBroadcast(new Intent("com.test.app"));
从Android9开始,它被限制处理其他应用程序发送的其他外部通知。 当它发出火灾通知时,您无法访问该方法。