2025年4月1日 星期二 乙巳(蛇)年 正月初二 设为首页 加入收藏
rss
您当前的位置:首页 > 计算机 > 编程开发 > 安卓(android)开发

Android 如何提高通知的有效率

时间:12-14来源:作者:点击数:7

Android 手机通过通知提高日活增加应用的新鲜度是很有帮助的,但是 Android 手机同时支持开启和关闭通知,但是设置关闭之后就无法接收通知,为了更好的提高日活增加新鲜度,可以判断是否关闭通知,提示用户打开通知。

Android SDK_INT 在高于 Build.VERSION_CODES.KITKAT(19) 可以判断通知是否被关闭。如下:

  • 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) {
  • try {
  • if (Build.VERSION.SDK_INT>= Build.VERSION_CODES.KITKAT) {
  • 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;
  • 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) {
  • }
  • return true;
  • }
方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门
本栏推荐