Java Code Examples for android.app.NotificationManager#areNotificationsEnabled()

The following examples show how to use android.app.NotificationManager#areNotificationsEnabled() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ToastUtils.java    From ToastUtils with Apache License 2.0 6 votes vote down vote up
/**
 * 检查通知栏权限有没有开启
 * 参考 SupportCompat 包中的方法: NotificationManagerCompat.from(context).areNotificationsEnabled();
 */
private static boolean areNotificationsEnabled(Context context) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        return manager != null && manager.areNotificationsEnabled();
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        AppOpsManager appOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
        ApplicationInfo appInfo = context.getApplicationInfo();
        String packageName = context.getApplicationContext().getPackageName();
        int uid = appInfo.uid;

        try {
            Class<?> appOpsClass = Class.forName(AppOpsManager.class.getName());
            Method checkOpNoThrowMethod = appOpsClass.getMethod("checkOpNoThrow", Integer.TYPE, Integer.TYPE, String.class);
            Field opPostNotificationValue = appOpsClass.getDeclaredField("OP_POST_NOTIFICATION");
            int value = (Integer) opPostNotificationValue.get(Integer.class);
            return ((int) checkOpNoThrowMethod.invoke(appOps, value, uid, packageName) == AppOpsManager.MODE_ALLOWED);
        } catch (ClassNotFoundException | NoSuchMethodException | NoSuchFieldException
                | InvocationTargetException | IllegalAccessException | RuntimeException ignored) {
            return true;
        }
    } else {
        return true;
    }
}
 
Example 2
Source File: NotificationSpecialOperation.java    From PermissionAgent with Apache License 2.0 5 votes vote down vote up
@Override
public boolean checkPermission(Context context) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        return notificationManager.areNotificationsEnabled();
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        return PermissionUtil.checkOpNoThrow(context, Const.OP_POST_NOTIFICATION);
    }
    return true;
}
 
Example 3
Source File: NotificationSpecialOperation.java    From PermissionAgent with Apache License 2.0 5 votes vote down vote up
@Override
public boolean checkPermission(Context context) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        return notificationManager.areNotificationsEnabled();
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        return PermissionUtil.checkOpNoThrow(context, Const.OP_POST_NOTIFICATION);
    }
    return true;
}
 
Example 4
Source File: LaunchModeManager.java    From Phantom with Apache License 2.0 5 votes vote down vote up
private boolean hasNotificationRights() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        NotificationManager nm = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
        return null != nm && nm.areNotificationsEnabled();
    }

    return true;
}
 
Example 5
Source File: AutoDiagnosisActivity.java    From FreezeYou with Apache License 2.0 5 votes vote down vote up
private void checkNotifyPermission(List<Map<String, Object>> problemsList) {
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    if (notificationManager == null || (Build.VERSION.SDK_INT >= 24 && !notificationManager.areNotificationsEnabled())) {
        problemsList.add(generateHashMap(getString(R.string.noNotifyPermission), getString(R.string.mayCannotNotify), "6", R.drawable.ic_attention));
    } else {
        problemsList.add(generateHashMap(getString(R.string.noNotifyPermission), getString(R.string.mayCannotNotify), "6", R.drawable.ic_done));
    }
}