Android适配

/* Android 6.0 版移除了对 Apache HTTP 客户端的支持,
    从 Android 9 开始,该内容库已从 bootclasspath 中移除,且默认情况下应用无法使用它
    拥有的最低 SDK 为 23 或更低的应用需要有 android:required="false" 属性,
    因为在 API 级别低于 24 的设备上,org.apache.http.legacy 库不可用。(在这些设备上,Apache HTTP 类在 bootclasspath 中提供。)*/
在AndroidManifest.xml的application标签内添加
<uses-library android:name="org.apache.http.legacy" android:required="false" />
/* Android 7.0,Android 框架执行的 StrictMode API 政策禁止在您的应用外部公开 file:// URI。
    如果一项包含文件URI的intent 离开您的应用,则应用出现故障,并出现 FileUriExposedException 异常。
要在应用间共享文件,您应发送一项 content:// URI,并授予 URI 临时访问权限。进行此授权的最简单方式是使用 FileProvider 类。 */
在AndroidManifest.xml的application标签内添加(com.xx.xx是应用包名)
        <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="com.xx.xx.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true"
            tools:replace="name,authorities,exported,grantUriPermissions">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths"
                tools:replace="name,resource" />
        </provider>

资源文件res下新建xml文件夹,添加file_paths.xml文件
<?xml version="1.0" encoding="utf-8"?>
<paths>
    <!-- 代表设备的根目录new File("/")-->
    <root-path name="camera_photos" path="" />
    <!--代表的目录即为:Environment.getExternalStorageDirectory()/Android/data/包名/-->
    <external-path name="files_root" path="Android/data/com/xx/xx/" />
    <!--代表的目录即为:Environment.getExternalStorageDirectory()-->
    <external-path name="external_storage_root" path="." />
</paths>

代码适配
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    Uri uri = FileProvider.getUriForFile(context, "app的包名.fileProvider", file);
} else {
    Uri uri = Uri.fromFile(file);
}
/* Android 8开始,通知需要绑定一个通道,不然就弹不出通知 */
        //创建一个通知管理器
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            //创建 通知通道  channelid和channelname是必须的(自己命名就好)
            NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_DEFAULT);
            mNotificationManager.createNotificationChannel(channel);
        }
        mNotificationManager.notify(1, notification);

 

/* Android 9,默认启用网络传输层安全协议 (TLS)
    如果您的应用以 Android 9 或更高版本为目标平台,
    则 isCleartextTrafficPermitted() 方法默认返回 false。
    如果您的应用需要针对特定网域启用明文,则您必须在应用的网络安全配置中,
    针对这些网域明确将 cleartextTrafficPermitted 设置为 true */

在AndroidManifest.xml的application标签设置
android:networkSecurityConfig="@xml/network_security_config"

资源文件res下新建xml文件夹,添加network_security_config.xml文件
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config cleartextTrafficPermitted="true" />
</network-security-config>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值