Gui*_*rot 92 android android-notifications android-notification-bar
Android 4.1为用户提供了一个复选框,用于禁用特定应用程序的通知.
但是,作为开发人员,我们无法知道对通知的调用是否有效.
我真的需要检查当前应用程序是否禁用了通知,但我在API中找不到任何设置.
有没有办法在代码中检查此设置?
Blu*_*ell 140
你不能100%不能.
在此Google I/O 2012视频中询问,并且新通知的项目负责人声明您不能.
2016年更新:现在您可以查看它,如此Google I/O 2016视频中所述.
NotificationManagerCompat.areNotificationsEnabled()从支持库中使用,检查API 19+上是否阻止了通知.API 19下面的版本将返回true(启用通知).
des*_*aci 39
实际上这很容易做到:
/**
* Created by desgraci on 5/7/15.
*/
public class NotificationsUtils {
private static final String CHECK_OP_NO_THROW = "checkOpNoThrow";
private static final String OP_POST_NOTIFICATION = "OP_POST_NOTIFICATION";
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;
}
}
Run Code Online (Sandbox Code Playgroud)
Pra*_*ash 33
来自@blundell的回答是正确的,但新版本有一些细微的变化.
NotificationManagerCompat.from(context).areNotificationsEnabled(??)
Run Code Online (Sandbox Code Playgroud)
如果您正在使用 Xamarin 并且需要此答案,则可以使用以下代码:
//return true if this option is not supported.
public class NotificationsUtils
{
private const String CHECK_OP_NO_THROW = "checkOpNoThrow";
private const String OP_POST_NOTIFICATION = "OP_POST_NOTIFICATION";
public static bool IsNotificationEnabled(global::Android.Content.Context context) {
AppOpsManager mAppOps = (AppOpsManager) context.GetSystemService(global::Android.Content.Context.AppOpsService);
ApplicationInfo appInfo = context.ApplicationInfo;
String pkg = context.ApplicationContext.PackageName;
int uid = appInfo.Uid;
try {
var appOpsClass = Java.Lang.Class.ForName("android.app.AppOpsManager");
var checkOpNoThrowMethod = appOpsClass.GetMethod(CHECK_OP_NO_THROW,Java.Lang.Integer.Type,Java.Lang.Integer.Type,new Java.Lang.String().Class);//need to add String.Type
var opPostNotificationValue = appOpsClass.GetDeclaredField (OP_POST_NOTIFICATION);
var value = (int)opPostNotificationValue.GetInt(Java.Lang.Integer.Type);
var mode = (int)checkOpNoThrowMethod.Invoke(mAppOps,value, uid, pkg);
return (mode == (int)AppOpsManagerMode.Allowed);
} catch (Exception)
{
System.Diagnostics.Debug.WriteLine ("Notification services is off or not supported");
}
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
53645 次 |
| 最近记录: |