我对此进行了很多研究,但找不到一个可行的解决方案。我希望我的应用程序不是通过按钮或其他方式而是自动禁用所有通知。我正在开发一个具有静音屏幕的应用程序,即屏幕是黑色的,但通知让我感到害怕。我已经尝试过 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"));