Android版本适配(6.0到9.0)

之前项目targetSdk基于Android6.0即API版本号为23进行开发,现在需要升级到9.0,期间跨越了好几个版本需要进行适配,那没有办法,动手开干,现在将适配所需要注意的点记录下来。

7.0适配

文件共享适配

1.首先需要在AndroidManifest.xml文件中,添加provider节点


    

2.然后要在res目录文件夹下创建xml文件夹,并且创建file_paths.xml文件




    
    
    


在paths节点内部支持以下几个子节点,分别为:

  • 代表设备的根目录new File("/")
  • 代表context.getFilesDir()
  • 代表context.getCacheDir()
  • 代表Environment.getExternalStorageDirectory()
  • 代表context.getExternalFilesDirs()
  • 代表getExternalCacheDirs()

每个节点都支持两个属性:

  • name
  • path
    path即为代表目录下的子目录,比如:

代表的目录即为:Environment.getExternalStorageDirectory()/download,其他同理。


8.0适配

Android8.0通知栏适配

Android8.0的通知栏,如果你的targetSdk在26及其以上,没有进行适配的话,即使你的应用开启了通知,通知栏也不会显示

具体设置为在你项目Application中添加以下代码在onCreate()方法中进行调用

private void initMessageChannel() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        String channelId = "channelId";
        String channelName = "channelName";
        String description = "description";
        // 通知的级别
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
        channel.setDescription(description);
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        assert notificationManager != null;
        notificationManager.createNotificationChannel(channel);

    }
}

通知的级别一共有四类,你可以根据你具体的需求进行设置,可以设置多个级别的通知,默认级别即使你不设置也会存在。

  • IMPORTANCE_MIN 开启通知,不会弹出,但没有提示音,状态栏中无显示
  • IMPORTANCE_LOW 开启通知,不会弹出,不发出提示音,状态栏中显示
  • IMPORTANCE_DEFAULT 开启通知,不会弹出,发出提示音,状态栏中显示
  • IMPORTANCE_HIGH 开启通知,会弹出,发出提示音,状态栏中显示

8.0不能自动安装APK问题适配

在Android8.0之后,未知应用安装权限默认关闭,且权限入口隐藏。

1.必须要在清单文件中添加权限


2.判断8.0以上系统是否有安装应用权限,并且进行适配

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    // 判断是否有安装的权限
    // 这里需要注意:targetSdkVersion是26以上才能获取正确的canRequestPackageInstalls,否则就一直返回false
    boolean hasInstallPermission = context.getPackageManager().canRequestPackageInstalls();
    if (!hasInstallPermission) {
        Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        ((Activity)context).startActivityForResult(intent,REQUEST_CODE_APP_INSTALL);
        return;
    }
 }else{
    install(context,filePath)
 }

// 安装APK的方法
public static void install(Context context, String filePath) {
    try {
        File file = new File(filePath);
        Intent intent = new Intent(Intent.ACTION_VIEW);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            Uri apkUri = FileProvider.getUriForFile(context, "com.jinr.core.provider", file);
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
        } else {
            intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        context.startActivity(intent);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

8.0系统弹框(悬浮框)不生效问题适配

8.0中应用只能使用TYPE_APPLICATION_OVERLAY窗口类型来创建悬浮窗,其它窗口类型在8.0已经被废弃掉。
如果应用中存在以下几种窗口类型来创建悬浮框,都需要进行适配,因为8.0已经不支持了。

  • TYPE_PHONE
  • TYPE_PRIORITY_PHONE
  • TYPE_SYSTEM_ALERT
  • TYPE_SYSTEM_OVERLAY
  • TYPE_SYSTEM_ERROR
  • TYPE_TOAST

在用到这些类型的悬浮框的时候,需要加上版本判断

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    window.setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
}else {
    window.setType(WindowManager.LayoutParams.TYPE_TOAST);
}

8.0静态广播无法使用适配

Android8.0中,Google已经明确提出应用无法使用其清单文件注册的大部分隐式广播,即我们常说的静态广播。
所以最好所用到的广播,都进行动态注册。


9.0适配

9.0禁止网络明文传输适配

Android9.0禁止网络明文传输,需要进行适配

1.在res目录文件夹下创建xml文件夹,并且创建network_security_config.xml文件



    

***2.在AndroidManifest.xml文件中,application节点下添加以下代码



    
        ...
    

该种方法既可以支持7.0抓包,也可以支持9.0明文请求

当然如果不需要抓包的功能,还有一种简单的方法进行配置,直接在清单文件中加入android:usesCleartextTraffic="true"属性即可



    
        ...
    


9.0 Apache Http请求出错适配

项目中如果使用Volley的话,在android 9.0会出现异常,提示ProtocolVersion的异常、

AndroidManifest.xml文件下的application节点下添加以下代码即可



    
        
        ...
    


9.0使用前台服务适配

在Android 9.0上使用前台服务,需要添加权限



    



总结

当然,这里只是列出来部分常见的适配,具体碰到的别的问题,还得另外进行适配。以后再遇到再进行补充吧。

你可能感兴趣的:(Android版本适配(6.0到9.0))