private void installNewApk(String outputFile) {
Uri uri = FileProvider.getUriForFile(getApplicationContext(), BuildConfig.APPLICATION_ID + ".provider", new File(outputFile + "app-demo-debug.apk"));
Intent install = new Intent(Intent.ACTION_VIEW);
install.setDataAndType(uri, "application/vnd.android.package-archive");
install.putExtra(Intent.ACTION_INSTALL_PACKAGE, true);
install.putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true);
install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
install.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(install);
}
My apk is not corrupted file because manually my apk install but programmatically not install.
Unknown Source already allow
please provide the answer
Related
I am trying to add option to update app by downloading apk, its getting installed successfully. But the problem is, app got close after installation, without prompting Open/Done screen in Nougat and above devices.
I have tried using both ACTION_VIEW and ACTION_INSTALL_PACKAGE, but no luck. Also tried with startActivityForResult instead of startActivity, still no luck.
public void installAPK() {
Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
File file = new File(path + "update.apk");
if (Build.VERSION.SDK_INT < 24) {
intent.setDataAndType(Uri.fromFile(new File(path + "update.apk")), "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // without this flag android returned a intent error!
} else {
Uri apkURI = FileProvider.getUriForFile(
activity,
activity.getApplicationContext()
.getPackageName() + ".provider", file);
intent.setDataAndType(apkURI, "application/vnd.android.package-archive");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
intent.putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true);
activity.startActivityForResult(intent, RC_INSTALL_APK);
}
Anything I missed to do?
Here's sample code of my application method to open new version
void OpenNewVersion(String location) {
Intent downloadIntent;
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
String PATH = Environment.getExternalStorageDirectory() + "/Download/";
File fileLocation = new File(PATH, "app-stock.apk");
Uri apkUri = FileProvider.getUriForFile(this, "Adapters.GenericFileProvider", fileLocation);
downloadIntent = new Intent(Intent.ACTION_VIEW);
downloadIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
downloadIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
downloadIntent.setDataAndType(apkUri, "application/vnd.android.package-archive");
List<ResolveInfo> resInfoList = this.getPackageManager().queryIntentActivities(downloadIntent, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolveInfo : resInfoList) {
String packageName = resolveInfo.activityInfo.packageName;
this.grantUriPermission(packageName, apkUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
} else {
File fileLocation = new File(this.getFilesDir(), "app-stock.apk");
downloadIntent = new Intent(Intent.ACTION_VIEW);
downloadIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
downloadIntent.setDataAndType(Uri.fromFile(fileLocation), "application/vnd.android.package-archive");
}
this.startActivity(downloadIntent);
}
My app has Auto-Update feature. I install apk file using this code:
Uri uri;
Intent intent;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
uri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider", file);
intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
intent.setData(uri);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
} else {
uri = Uri.fromFile(file);
intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
context.startActivity(intent);
The problem is that in api 24 or higher Android package installer doesn't ask to click on "Done" or "Open" after updating completed and dialog will be closed without any questions or any messages.
I need package installer ask what to do like
.
My App is distributed outside of Play Store and it is upgraded within the App when there is a new version available. I am using the below code to start the install (after the file was downloaded using DownloadManager)
String destination = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/app.apk";
For Android N (24):
Uri contentUri = FileProvider.getUriForFile(ctxt, BuildConfig.APPLICATION_ID + ".provider", new File(destination));
Intent openFileIntent = new Intent(Intent.ACTION_VIEW);
openFileIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
openFileIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
openFileIntent.setData(contentUri);
startActivity(openFileIntent);
For Android 23 and below:
final Uri uri = Uri.parse("file://" + destination);
Intent install = new Intent(Intent.ACTION_VIEW);
install.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
install.setDataAndType(uri,"application/vnd.android.package-archive");
startActivity(install);
When I use the same code for "Android N", it is not showing the system install activity in Android Oreo. Anyone who has solved this problem?
I am programatically installing APK and using following code for that :
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Uri apkUri = FileProvider.getUriForFile(getApplicationContext(),
BuildConfig.APPLICATION_ID + ".fileprovider", new File(Environment
.getExternalStorageDirectory() + "/.QCApp/" +
Constants.APP_TYPE + versionName + ".apk"));
Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
intent.setData(apkUri);
intent.putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
} else {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() +
"/.QCApp/" +
Constants.APP_TYPE + versionName + ".apk")),
"application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
But in Android Nougat APK getting successfully installed but "Done" and "Open" screen is not appearing.
What I'm missing ?
This is how i install a apk file:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Now i want to install a apk file from sdcard, i'm getting the uri through Storage Access Framework (From Kitkat), but code does not working.
Uri uri1 = Uri.fromFile(file); //file class File
Uri uri2 = df.getUri(); //df class DocumentFile
The code with uri1 works, but not with uri2
Any workaround?
Thanks