前不久因为API26(Android 8.0)以上的设备无法显示通知栏,所以网上查阅了比较多了资料,得出结论,API26以后引入了通知渠道这么个东西,然后就写了一篇问题对应的博文:Android 8.0通知栏(Notification)适配,在模拟上是完美运行,可是当我前两天安装到真机上测试时,咦?怎么又无效了?然后我就想着,是不是通知权限没打开?因为模拟器上很多权限的控制和真机上差别很大,我打开设置一看,还真是!那么本文就接着Android 8.0通知栏(Notification)适配作一个补充。 我们要打开通知 管理页面 第一步当然是判断 通知权限是否打开了? ,打开了我们就不需要跳转到通知管理页面了,没打开我们才进行跳转。 这里我们用NotificationManagerCompat提供的 .areNotificationsEnabled() 方法来判断通知权限是否打开,返回的是boolean值 注意:这个方法只适用于API19(Android 4.4)以上,API19以下会直接返回true,当然我们不是特殊开发也可以忽略了,毕竟现在市场上低于 Android4.4 的手机恐怕不好找,而且貌似Android4.4以下的对权限这块也没有太严格~ 我们来看看 areNotificationsEnabled() 的源码,源码可以很清晰明朗地看到,API24以上直接调用 通知权限判断完了当然是要根据判断结果来进行跳转了,上面我们可以知道 这里补充一下 Android 8.0 +的 Notification通知栏消息 的使用,相对于以前也只是加了一个NotificationChannel通知渠道,想要详细看的可以去我前一篇关于通知栏的博客:Android 8.0通知栏(Notification)适配 如果文章对你还算有点帮助,点个赞吧!文章目录
Android 10 真机测试效果:
判断通知权限是否打开
一行代码判断
NotificationManagerCompat.from(this).areNotificationsEnabled()
areNotificationsEnabled() 的源码(可忽略)
mNotificationManager.areNotificationsEnabled();
方法,而 API19 -API 23 则是通过反射的方式来获取,API 19 以下就直接返回 true 了 /** * Returns whether notifications from the calling package are not blocked. */ public boolean areNotificationsEnabled() { if (Build.VERSION.SDK_INT >= 24) { return mNotificationManager.areNotificationsEnabled(); } else if (Build.VERSION.SDK_INT >= 19) { AppOpsManager appOps = (AppOpsManager) mContext.getSystemService(Context.APP_OPS_SERVICE); ApplicationInfo appInfo = mContext.getApplicationInfo(); String pkg = mContext.getApplicationContext().getPackageName(); int uid = appInfo.uid; try { Class<?> 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(appOps, value, uid, pkg) == AppOpsManager.MODE_ALLOWED); } catch (ClassNotFoundException | NoSuchMethodException | NoSuchFieldException | InvocationTargetException | IllegalAccessException | RuntimeException e) { return true; } } else { return true; } }
跳转到通知管理页面
NotificationManagerCompat.from(this).areNotificationsEnabled()
是会返回 boolean 值的,true 代表权限已经打开,反之则没有打开,这里我们可以写一个 if 语句根据返回结果来进行判断,这里直接给一个现成的方法吧方法逻辑:
private boolean intent(Context context) {//判断应用的通知权限是否打开,返回Boolean值 if (!NotificationManagerCompat.from(context).areNotificationsEnabled()) { Intent localIntent = new Intent(); //判断API,跳转到应用通知管理页面 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {//8.0及以上 localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS"); localIntent.setData(Uri.fromParts("package", context.getPackageName(), null)); } else (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//8.0以下 localIntent.setAction("android.settings.APP_NOTIFICATION_SETTINGS"); localIntent.putExtra("app_package", context.getPackageName()); localIntent.putExtra("app_uid", context.getApplicationInfo().uid); } context.startActivity(localIntent); return false; } return true; }
补充
方法逻辑
public void show(Context context, String title, String msg) { if (intent(context)) { NotificationManager manager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE); Notification notification = null; if (Build.VERSION.SDK_INT >= 26) {//API26以上的方法 NotificationChannel channel = new NotificationChannel("id", "name", NotificationManager.IMPORTANCE_HIGH); manager.createNotificationChannel(channel); notification = new Notification.Builder(context, "id") .setContentTitle(title) .setContentText(msg) .setSmallIcon(R.mipmap.img_icon) .build(); } else {//API26以下 notification = new Notification.Builder(context) .setContentTitle(title) .setContentText(msg) .setSmallIcon(R.mipmap.img_icon) .build(); } manager.notify(1, notification); } }
本网页所有视频内容由 imoviebox边看边下-网页视频下载, iurlBox网页地址收藏管理器 下载并得到。
ImovieBox网页视频下载器 下载地址: ImovieBox网页视频下载器-最新版本下载
本文章由: imapbox邮箱云存储,邮箱网盘,ImageBox 图片批量下载器,网页图片批量下载专家,网页图片批量下载器,获取到文章图片,imoviebox网页视频批量下载器,下载视频内容,为您提供.
阅读和此文章类似的: 全球云计算