Reputation: 317
I want to install the program I downloaded from my program, but when I put targetSdkVersion 25 in build.gradle file, the apk is installed, but when I put targetSdkVersion in 28 in build gradle file, the apk will not be installed , and the program runs until the startActivity(intent) line but is not installed , and there are no error messages in logcat.
File path APK
"/sdcard/Android/data/com.****.****/Version/update.apk"
in Manifests
<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>
in 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>
in build.gradle file
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
}
in java for install 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());
}
}
Upvotes: 1
Views: 1955
Reputation: 317
first add permission in Manifests file
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
in java for install 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());
}
}
Upvotes: 1