【发布时间】:2018-11-09 06:29:11
【问题描述】:
我想安装从我的程序下载的程序,但是当我将 targetSdkVersion 25 放入 build.gradle 文件时,apk 是已安装,但是当我将 targetSdkVersion in 28 放入构建 gradle 文件时,apk 将不会安装 em>,程序一直运行到startActivity(intent)这一行但没有安装,logcat中也没有报错信息。
文件路径 APK
"/sdcard/Android/data/com.****.****/Version/update.apk"
在清单中
<application
.....>
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.authorityStr"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
</application>
在 XML provider_paths 中
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path
name="sdcard"
path="Android/data/com.****.*****/Version/"/>
</paths>
在 build.gradle 文件中
compileSdkVersion 28
defaultConfig {
applicationId "*****"
minSdkVersion 15
targetSdkVersion 28 //when change to targetSdkVersion 25 apk installed
versionCode 1
versionName "1.5"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
signingConfig signingConfigs.config
}
在java中安装APK
public static void InstallApk(String filename) {
try {
File file = new File(filename);
if (file.exists()) {
if (Build.VERSION.SDK_INT > 22) {
Uri fileUri = Uri.fromFile(file);
if (Build.VERSION.SDK_INT >= 24) {
fileUri = FileProvider.getUriForFile(Context, Context.getPackageName() + ".authorityStr",
file);
}
Intent intent = new Intent(Intent.ACTION_VIEW, fileUri);
intent.putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true);
intent.setDataAndType(fileUri, "application/vnd.android" + ".package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
Context.startActivity(intent);
} else {
Intent install = new Intent(Intent.ACTION_VIEW);
install.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Context.startActivity(install);
}
}
} catch (Exception ex) {
Log.d(Variable.TagMessage, ex.getMessage());
}
}
【问题讨论】:
-
logcat 中的任何内容?
-
程序运行到 startActivity(intent) 行但没有安装。手机是安卓8.1。同一行代码在 Android 6 上运行,logcat 中没有错误信息。代码中是否存在问题或错误? @VladyslavMatviienko
-
你有解决这个问题的办法吗?
-
不,你有解决办法吗? @AbhishekBardolia
标签: android download sdk installation failed-installation