1、项目中的targetSdkVersion改为 28
2、android 9.0默认使用的是网络传输层安全协议(TLS),https代替http.
网络适配:
- 在 res 目录下新建xml文件夹,添加network_security_config.xml文件
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true" />
</network-security-config>
- AndroidManifest.xml中的application添加:
<?xml version="1.0" encoding="utf-8"?>
<manifest ... >
<application android:networkSecurityConfig="@xml/network_security_config">
...
</application>
</manifest>
也可以指定适配http域名
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<!-- Android 9.0 上部分域名时使用 http -->
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">secure.example.com</domain>
<domain includeSubdomains="true">cdn.example1.com</domain>
</domain-config>
</network-security-config>
可以看官方文档
3. Apache HTTP适配
android 6.0开始已经不再支持Apache HTTP客户端,如果想继续使用需要在androidManifest.xml中配置。
<uses-library android:name="org.apache.http.legacy" android:required="false"/>
- 服务(service)适配
高版本启动服务使用startForegroundService 为前台服务。
Intent intentService = new Intent(this, MyService.class);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
startForegroundService(intentService);
} else {
startService(intentService);
}
android 9.0需要另外添加权限:
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
- 启动activity
android 9.0不能直接在service、application(非activity)中启动activity.
解决办法:
Intent intent = new Intent(this, TestActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
-
屏幕适配
市面上出现很多刘海屏、水滴屏、挖孔屏、美人尖。。。
在Android 9.0中官方提供了DisplayCutout 类,可以确定刘海区域的位置。参考:Android刘海屏、水滴屏全面屏适配方案 -
权限
在9.0 中新增权限组CALL_LOG 并将 READ_CALL_LOG、WRITE_CALL_LOG 和 PROCESS_OUTGOING_CALLS 权限从PHONE中移入该组。
1.限制访问通话记录
如果应用需要访问通话记录或者需要处理去电,则您必须向 CALL_LOG权限组明确请求这些权限。 否则会发生 SecurityException。
2.限制访问电话号码
要通过 PHONE_STATE Intent 操作读取电话号码,同时需要 READ_CALL_LOG 权限和 READ_PHONE_STATE 权限。
要从 PhoneStateListener的onCallStateChanged() 中读取电话号码,只需要 READ_CALL_LOG 权限。 不需要 READ_PHONE_STATE 权限。
- 获取硬件系列号
在 Android 9 中,调用Build.SERIAL 会始终返回 UNKNOWN 以保护用户的隐私。如果你的应用需要访问设备的硬件序列号,那么需要先请求 READ_PHONE_STATE 权限,然后调用 Build.getSerial()。
掘金:https://juejin.im/post/6844903906942648334
https://juejin.im/post/6844903859823837191