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)
只会提示一次不会烦人的一直响,还挺省事

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 179,319评论 25 708
  • 用两张图告诉你,为什么你的 App 会卡顿? - Android - 掘金 Cover 有什么料? 从这篇文章中你...
    hw1212阅读 14,096评论 2 59
  • 2017.050523:14 打开App 公司:淮北慕思寝具。 226 期 苏州 感谢一组学员 ...
    慕思寝具阅读 175评论 0 0
  • 菠萝呦阅读 186评论 0 0
  • 我是哲达,喜欢交友,喜欢旅行,喜欢思考,更喜欢简单,拥有九零后专属的笑容和美丽的梦想。高中毕业误打误撞报上心理学专...
    哲达阅读 425评论 1 3

友情链接更多精彩内容