我想安装从我的程序下载的程序,但是当我将buildSdkVersion 25放在build.gradle文件中时,安装了apk,但是当我将buildSdkVersion放入构建gradle文件中的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());
}
}
首先在Manifest文件中添加权限
<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());
}
}