我想安装从程序中下载的程序,但是当我将 targetSdkVersion 25 放入build.gradle文件中时, apk是已安装 ,但是当我在构建gradle文件中将 targetSdkVersion放入28 时,将不会安装 apk ,程序将运行到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());
}
}
答案 0 :(得分:0)
首先在清单文件中添加权限
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
在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 |
Intent.FLAG_GRANT_READ_URI_PERMISSION);
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());
}
}