检查是否为特定包启用通知 [英] checking if notifications are enabled for specific package
本文介绍了检查是否为特定包启用通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在开发一个系统应用程序,我需要从我的应用程序中检查某个包是否启用了通知.
I'm developing a system application and I need to check from my application if a certain package has notifications enabled or not.
我一直在尝试这个代码:
I've been triying this code:
public static boolean isNotificationEnabled(Context context) {
AppOpsManager mAppOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
ApplicationInfo appInfo = context.getApplicationInfo();
String pkg = context.getApplicationContext().getPackageName();
int uid = appInfo.uid;
Class appOpsClass = null; /* Context.APP_OPS_MANAGER */
try {
appOpsClass = Class.forName(AppOpsManager.class.getName());
Method checkOpNoThrowMethod = appOpsClass.getMethod(CHECK_OP_NO_THROW, Integer.TYPE, Integer.TYPE, String.class);
Field opPostNotificationValue = appOpsClass.getDeclaredField(OP_POST_NOTIFICATION);
int value = (int)opPostNotificationValue.get(Integer.class);
return ((int)checkOpNoThrowMethod.invoke(mAppOps,value, uid, pkg) == AppOpsManager.MODE_ALLOWED);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return false;
}
问题是它一直在返回 true.我做错了什么?
The issue is that it is returning true all the time. What am I doing wrong?
推荐答案
如果您的方法获取目标应用程序的Context
(您要检查是否启用通知).
If your method gets Context
of the target application (which you want to check if notifications enabled).
您可以基于此 Context
创建 NotificationManagerCompat
实例并检查是否为该应用程序启用了通知:
You can create NotificationManagerCompat
instance based on this Context
and check if notifications are enabled for that application:
public static boolean isNotificationEnabled(Context context) {
return NotificationManagerCompat.from(context.getApplicationContext())
.areNotificationsEnabled();
}
这篇关于检查是否为特定包启用通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文