android各版本的兼容问题
16lz
2021-01-23
自动安装
在Android7.0自动安装做出了修改,android8.0增加了权限
//以前 Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive"); startActivity(intent);
//android 7.0需要用到共享文件provider的方式,不能识别file://需要将其转为uriIntent intent = new Intent();intent.setAction(Intent.ACTION_VIEW);intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);Uri contentUri = FileProvider.getUriForFile(context, "com.zjhc.jxzq.jxzq.fileprovider", file); intent.setDataAndType(contentUri, "application/vnd.android.package-archive"); startActivity(intent); //AndroidManifist.xml中配置 //res下新建xml目录,新建file_paths.xml文件<?xml version="1.0" encoding="utf-8"?> 注意:保存路径需要新建apk目录,Environment.getExternalStorageDirectory()对应external-path
//android8.0需要写入权限 //权限校验 boolean b =getPackageManager().canRequestPackageInstalls(); if(!b){ ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.REQUEST_INSTALL_PACKAGES}, 1000); }
开启服务
android 8.0开启前台服务,正对startService()更改成使用startForegroundService()。
现场保活时,系统对电量进行了进一步的优化,如果不考虑点亮可以尝试将应用加入电量优化的白名单
if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.O){ PowerManager powerManager = (PowerManager) activity.getSystemService(POWER_SERVICE); boolean hasIgnored = powerManager.isIgnoringBatteryOptimizations(activity.getPackageName()); // 判断当前APP是否有加入电池优化的白名单,如果没有,弹出加入电池优化的白名单的设置对话框。 if (!hasIgnored) { Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS); intent.setData(Uri.parse("package:" + activity.getPackageName())); activity.startActivity(intent); } }
8.0使用startForegroundService需要配一个Notification,不然报Context.startForegroundService() did not then call Service.startForeground()错误
更多相关文章
- Android 改变屏幕亮度时需添加权限
- 初学Android,电池电量提示(七十五)
- Android 获取手机电量信息
- Android应用程序获取ROOT权限代码
- Android uses-permission 权限大全
- Android获取系统应用及安装应用的权限列表
- 如何在Android 11 中正确请求位置权限?以及Android 8 - 11位置权
- android M上可能需要开发者注意的权限大全