清除不可移除的通知
Clear Non Removable Notifications
我正在制作删除选定应用程序通知的通知拦截器。
我在我的通知侦听器服务中使用了该片段,
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
if(SettingsActivity.SettingsInfo.getInt(sbn.getPackageName(), R.drawable.unblock)==R.drawable.block)
{
if(Build.VERSION.SDK_INT<Build.VERSION_CODES.LOLLIPOP)
cancelNotification(sbn.getPackageName(), sbn.getTag(), sbn.getId());
else
cancelNotification(sbn.getKey());
}
}
但是此代码仅清除可取消 通知。但我想删除所选应用程序的所有通知(也包括系统通知)。有什么办法吗?
我也试过反射法。
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.cancel(sbn.getTag(), sbn.getId());
Class localClass = Class.forName("android.os.ServiceManager");
Method getService = localClass.getMethod("getService", new Class[]{String.class});
if (getService != null) {
Object result = getService.invoke(localClass, new Object[]{Context.NOTIFICATION_SERVICE});
if (result != null) {
IBinder binder = (IBinder) result;
final INotificationManager iNotificationManager = INotificationManager.Stub.asInterface(binder);
int uid = 0;
try {
uid = this.getPackageManager().getApplicationInfo(sbn.getPackageName(), 0).uid;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
iNotificationManager.cancelAllNotifications(sbn.getPackageName(), uid);
}
}
但我得到 Security Exception
as Calling UID gave package owned by other UID
更多详情,我想喜欢
App ->App Settings -> Notifications -> Block All
提前致谢!!!
如果你想取消其他应用的通知,你需要使用
NotificationListenerService
它有cancelNotification
方法:
public class NotificationListener extends NotificationListenerService {
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
cancelNotification(sbn.getKey());
}
}
试试这个,让我知道它是否有效:
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
public class NotificationService extends NotificationListenerService {
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
// Store each StatusBarNotification object
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
cancelNotification(sbn.getPackageName(), sbn.getTag(), sbn.getId());
Log.d("Cncel ","Cancel not");
}
else {
cancelNotification(sbn.getKey());
Log.d("key ","Cancel not key");
}
}
@Override
public void onNotificationRemoved(StatusBarNotification sbn) {
// Delete removed StatusBarNotification objects
}
}
感谢您的回复,根据我的研究我找到了解决方案
to clear notifications with flag FLAG_NO_CLEAR
对于 api>25,您可以延后通知
snoozeNotification(sbn.getKey(), Long.MAX_VALUE);
对于 api>23 和 <26 ,有一种方法可用于 ovveride group key 并将它们分组到您的通知中,
statusbarnotification.setOverrideGroupKey("key");
少api,
您需要 root 权限或系统权限。然后你可以使用
iNotificationManager.cancelAllNotifications(sbn.getPackageName(), uid);
我正在制作删除选定应用程序通知的通知拦截器。
我在我的通知侦听器服务中使用了该片段,
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
if(SettingsActivity.SettingsInfo.getInt(sbn.getPackageName(), R.drawable.unblock)==R.drawable.block)
{
if(Build.VERSION.SDK_INT<Build.VERSION_CODES.LOLLIPOP)
cancelNotification(sbn.getPackageName(), sbn.getTag(), sbn.getId());
else
cancelNotification(sbn.getKey());
}
}
但是此代码仅清除可取消 通知。但我想删除所选应用程序的所有通知(也包括系统通知)。有什么办法吗?
我也试过反射法。
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.cancel(sbn.getTag(), sbn.getId());
Class localClass = Class.forName("android.os.ServiceManager");
Method getService = localClass.getMethod("getService", new Class[]{String.class});
if (getService != null) {
Object result = getService.invoke(localClass, new Object[]{Context.NOTIFICATION_SERVICE});
if (result != null) {
IBinder binder = (IBinder) result;
final INotificationManager iNotificationManager = INotificationManager.Stub.asInterface(binder);
int uid = 0;
try {
uid = this.getPackageManager().getApplicationInfo(sbn.getPackageName(), 0).uid;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
iNotificationManager.cancelAllNotifications(sbn.getPackageName(), uid);
}
}
但我得到 Security Exception
as Calling UID gave package owned by other UID
更多详情,我想喜欢
App ->App Settings -> Notifications -> Block All
提前致谢!!!
如果你想取消其他应用的通知,你需要使用 NotificationListenerService
它有cancelNotification
方法:
public class NotificationListener extends NotificationListenerService {
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
cancelNotification(sbn.getKey());
}
}
试试这个,让我知道它是否有效:
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
public class NotificationService extends NotificationListenerService {
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
// Store each StatusBarNotification object
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
cancelNotification(sbn.getPackageName(), sbn.getTag(), sbn.getId());
Log.d("Cncel ","Cancel not");
}
else {
cancelNotification(sbn.getKey());
Log.d("key ","Cancel not key");
}
}
@Override
public void onNotificationRemoved(StatusBarNotification sbn) {
// Delete removed StatusBarNotification objects
}
}
感谢您的回复,根据我的研究我找到了解决方案
to clear notifications with flag FLAG_NO_CLEAR
对于 api>25,您可以延后通知
snoozeNotification(sbn.getKey(), Long.MAX_VALUE);
对于 api>23 和 <26 ,有一种方法可用于 ovveride group key 并将它们分组到您的通知中,
statusbarnotification.setOverrideGroupKey("key");
少api,
您需要 root 权限或系统权限。然后你可以使用
iNotificationManager.cancelAllNotifications(sbn.getPackageName(), uid);