App更新8.0适配出现的问题

问题1 android.os.FileUriExposedException

这个应该是7.0就应该处理的。新的权限机制

解决办法就是在AndroidManifest中配置

<provider
  
    android:name="android.support.v4.content.FileProvider"
    android:authorities="${applicationId}.provider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
      
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/provider_paths"/>
</provider>

然后在res创建对应的xml写入paths

<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path
    name="external_files"
    path="."/>
</paths>

拉起安装apk的时候变为

val uri: Uri
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    uri = FileProvider.getUriForFile(App.mInstance, App.mInstance.applicationContext.packageName + ".provider", apkFile)
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
}
else {
    uri = Uri.fromFile(apkFile)
}
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
intent.setDataAndType(uri, "application/vnd.android.package-archive")
startActivity(intent)

但是这样还是有可能会出现问题,比如当我们第三方库中有类似的配置可能就会出现错误,编译不通过

org.gradle.api.tasks.TaskExecutionException: Execution failed for task ‘:app:processDebugManifest’.

通过gradlew processDebugManifest –stacktrace查看具体的日志

  Attribute provider#android.support.v4.content.FileProvider@authorities value=(com.xpand.work.provider) from AndroidManifest.xml:110:13-58
        is also present at [com.jph.takephoto:takephoto_library:4.0.3] AndroidManifest.xml:19:13-64 value=(com.xpand.work.fileprovider).
        Suggestion: add 'tools:replace="android:authorities"' to <provider> element at AndroidManifest.xml:107:9-117:20 to override.

属性冲突了。以前经常遇到过的。

通过建议设置修改为这样就ok了

<provider
    tools:replace="android:authorities"
    android:name="android.support.v4.content.FileProvider"
    android:authorities="${applicationId}.provider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        tools:replace="android:resource"
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/provider_paths"/>
</provider>

重要更新

经过上面的代码我以为已经解决了问题,下载更新功能确实已经可用,但是又出现了新的问题。
测试的时候发现app拍照会出问题,takePhoto库的拍照取不到照片路径了。于是找了各种方案最后还是下面的文章助我解决了这个问题
参考 https://www.jianshu.com/p/88a3f8608d6f

            android:name=".view.custom.MyProvider"
            android:authorities="${applicationId}.app.provider"
            android:exported="false"
            tools:replace="name,authorities,exported,grantUriPermissions"
            android:grantUriPermissions="true">
            <meta-data
                tools:replace="name,resource"
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/app_file_paths"/>
        </provider>

问题2 通知栏适配

按照以前低版本的写法不能弹出通知栏下载进度条,因为8.0的通知变化构建

Builder的时候单context参数方法已被废弃

 /**
 * @deprecated use
 * {@link NotificationCompat.Builder#NotificationCompat.Builder(Context, String)} instead.
 * All posted Notifications must specify a NotificationChannel Id.
 */
public Builder(Context context) {
    this(context, null);
}

提示我们调用带channelid的构造方法进行初始化。需要channelId

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    createChanel()
}



@RequiresApi(Build.VERSION_CODES.O)
private fun createChanel() {
    val channel = NotificationChannel(CHANNELID, "process", NotificationManager.IMPORTANCE_HIGH)
    mBuilder!!.setDefaults(NotificationCompat.FLAG_ONLY_ALERT_ONCE)
    mNotifyManager!!.createNotificationChannel(channel)
}

问题3 不能弹出安装页面

8.0需要权限

<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>

才能弹出

“处于安全考虑,已禁止您的手机安装来自此来源的未知应用”

这段提示语,然后用户自己去设置开启允许安装此来源的应用,就可以安装了。

倒是不像网上说的还要像适配6.0的敏感权限一样去申请呢。

问题4 取消通知栏震动

下载apk文件的时候一直震动,想要关闭,网上很多博客说设置channel的这个属性

channel.enableVibration(false)
channel.vibrationPattern= longArrayOf(0)

但是在响铃模式会有声音一直响,所以干脆设置
mBuilder!!.setOnlyAlertOnce(true)
只会提示一次不会烦人的一直响,还挺省事

    原文作者:Avalon1
    原文地址: https://www.jianshu.com/p/08c623161c15
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞