Fix APK installation on old Android versions

This commit is contained in:
topjohnwu 2018-12-04 20:27:09 -05:00
parent 8d931dd773
commit 4084e8790b

View File

@ -11,18 +11,15 @@ import androidx.core.content.FileProvider;
public class APKInstall { public class APKInstall {
public static void install(Context c, File apk) { public static void install(Context c, File apk) {
Intent install = new Intent(Intent.ACTION_INSTALL_PACKAGE);
install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
install.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Intent install = new Intent(Intent.ACTION_INSTALL_PACKAGE); install.setData(FileProvider.getUriForFile(c, c.getPackageName() + ".provider", apk));
install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
install.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri content = FileProvider.getUriForFile(c, c.getPackageName() + ".provider", apk);
install.setData(content);
c.startActivity(install);
} else { } else {
Intent install = new Intent(Intent.ACTION_VIEW); apk.setReadable(true, false);
install.setDataAndType(Uri.fromFile(apk), "application/vnd.android.package-archive"); install.setData(Uri.fromFile(apk));
install.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
c.startActivity(install);
} }
c.startActivity(install);
} }
} }