Android Notification工具类
16lz
2021-01-23
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工程获取bulid.xml文件的方法
- [读书笔记]Android LayoutInflater.inflate方法参数详解原理分析
- Error occurred during initialization of VM解决方法(Android S
- Android中的使用ListView控件显示List集合中的数据,并对Item响应
- Android使用httpPost向服务器发送请求的方法
- 设置图片缩放方法失效 解决办法设置android:scaleType属性
- XAMARIN Android获取WIFIMAC地址的方法
- 实时获取Android 各版本电量的统一方法