App更新8.0適配出現的問題
問題1android.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>
問題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!!.setDefaults(NotificationCompat.FLAG_ONLY_ALERT_ONCE)
解決