[英]How to install Android system app programmactically
單擊“更新”按鈕時,我將安裝 android 系統應用程序。 但是我沒有找到合適的解決方案。
我在代碼中使用了命令“pm install ***.apk”。
我嘗試使用如下:
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);
但是這些代碼對我沒有幫助。
如果你曾經解決過這個問題,請幫助我。
這是從網站下載 APK 並提示用戶安裝它的完整代碼示例:
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));
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.