Android Notification工具类

1. 在有些情况下,要检测系统是否禁止app显示通知,可以调用一下方法

/**
 * 检查是否可以显示通知
 * @return
 */
  public boolean isNotificationEnabled() {
	AppOpsManager mAppOps = (AppOpsManager) mContext.getSystemService(Context.APP_OPS_SERVICE);
	ApplicationInfo appInfo = mContext.getApplicationInfo();
	String pkg = mContext.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 (Exception e) {
		e.printStackTrace();
	}
	return false;
  }


你可能感兴趣的:(android)