I am going to install the android system app when click "update" button. But I didn't find a suitable solution.
I used the command "pm install ***.apk" in the code.
And I tried to use as follows:
Intent in = new Intent(Intent.ACTION_VIEW);
in.setDataAndType("...apk name", "application/vnd.android.package-archive");
in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(in);
But these codes don't help me.
If you have ever solved this problem, please help me.
Here's the full code example that downloads the APK from a website and prompts the user to install it:
protected void installUpdate() {
String destination = getApplicationContext().getExternalFilesDir(DOWNLOAD_SERVICE) + "/";
String fileName = "update.apk";
destination += fileName;
final Uri uri = Uri.parse("file://" + destination);
File file = new File(destination);
if (file.exists()) {
file.delete();
}
DownloadManager.Request request = new DownloadManager.Request(Uri.parse("https://mylink.com/update.apk"));
request.setDescription("Update");
request.setTitle("MyApp");
request.setDestinationUri(uri);
final DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
BroadcastReceiver onComplete = new BroadcastReceiver() {
public void onReceive(Context ctxt, Intent intent) {
File toInstall = new File(getApplicationContext().getExternalFilesDir(DOWNLOAD_SERVICE), "update" + ".apk");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Uri apkUri = FileProvider.getUriForFile(getApplicationContext(), BuildConfig.APPLICATION_ID + ".provider", toInstall);
Intent intentUpdate = new Intent(Intent.ACTION_INSTALL_PACKAGE);
intentUpdate.setData(apkUri);
intentUpdate.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_ACTIVITY_NEW_TASK);
} else {
Uri apkUri = Uri.fromFile(toInstall);
Intent intentUpdate = new Intent(Intent.ACTION_VIEW);
intentUpdate.setDataAndType(apkUri, "application/vnd.android.package-archive");
intentUpdate.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
getApplicationContext().startActivity(intentUpdate);
toInstall.deleteOnExit();
getApplicationContext().unregisterReceiver(this);
stopSelf();
}
};
getApplicationContext().registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.