Android 8.0 通知栏不显示

由于 Google Play 现在限制了上传 APK 的 targetSdkVersion,所以在新项目中就开始把版本号升到最高,这样一来之前前人写的一些代码库就会出现兼容性的问题,比如下载更新 APK 时,FileProvider 问题(我上一篇有讲到过)。比如在下载的过程中,之前写好的通知栏中显示进度。但是在 targetSdkVersion > 26 时,会被屏蔽掉。

在 new NotificationCompat.Builder(context) 之前适配版本是否大于26 


 mNotifyManager = (NotificationManager) mContext
                        .getSystemService(Context.NOTIFICATION_SERVICE);
                
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

                    NotificationChannel channel = new NotificationChannel("channel_id", "channel", NotificationManager.IMPORTANCE_LOW);

                    channel.setLightColor(Color.GREEN);

                    channel.enableLights(true);

                    channel.enableVibration(true);

                    channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);

                    mNotifyManager.createNotificationChannel(channel);
                }

 另外 Android 8.0 还需要注意,添加 "允许未知来源" 权限,因为在下载安装 APK 时,高版本手机需要设置

  • 在 AndroidManifest.xml 添加权限
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
  • 下载完安装时,需要添加 Flags

        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);//检测是否允许未知来源
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setDataAndType(uri,
                "application/vnd.android.package-archive");
        context.startActivity(intent);

经过测试之后,终于更新了一个版本到市场。

发布了48 篇原创文章 · 获赞 10 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/jacksinrow/article/details/84030626