如何在Android Studio中以编程方式删除提醒通知?
我对此做了很多研究,但我找不到一个可行的解决方案。我希望我的应用程序能够自动禁用所有通知,而不是通过按钮或其他方式。我正在开发一个应用程序,它有一个无声的屏幕,也就是说,屏幕是黑色的,但是通知让我很害怕。我已经尝试过NotificationListener,这样无论何时发布,它都会自动取消它们,但对我不起作用。有没有天才能解决我的问题如何在Android Studio中以编程方式删除提醒通知?,android,service,android-notifications,notification-listener,notificationservices,Android,Service,Android Notifications,Notification Listener,Notificationservices,我对此做了很多研究,但我找不到一个可行的解决方案。我希望我的应用程序能够自动禁用所有通知,而不是通过按钮或其他方式。我正在开发一个应用程序,它有一个无声的屏幕,也就是说,屏幕是黑色的,但是通知让我很害怕。我已经尝试过NotificationListener,这样无论何时发布,它都会自动取消它们,但对我不起作用。有没有天才能解决我的问题 public class NotificationListenerEx extends NotificationListenerService { public
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>
从Android 9开始,它被限制处理其他应用程序发送的其他外部通知。您无法在发出火灾通知时访问该方法。那么解决方案是什么?同样的代码在安卓9之前的设备上也不起作用。它的访问问题现在不可用,现在受到限制
getApplicationContext().sendBroadcast(new Intent("com.test.app"));