可能重复:
Android:以编程方式安装.apk
我需要更新我的Android应用程序.在程序内部,我下载了新版本.如何通过下载(以编程方式)下载新版本来替换当前版本?
URL url = new URL("http://www.mySite.com/myFolder/myApp.apk");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
try
{
FileOutputStream fos = this.getApplicationContext().openFileOutput("myApp.apk", Context.MODE_WORLD_READABLE|Context.MODE_WORLD_WRITEABLE);
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(in, "UTF-8"));
StringBuilder sb = new StringBuilder();
byte[] buffer = new byte[8192];
int len;
while ((len = in.read(buffer)) != -1)
{
// EDIT - only write the bytes that have been written to
// the buffer, not the whole buffer
fos.write(buffer, 0, len); // file to save app
}
fos.close(); …Run Code Online (Sandbox Code Playgroud) 我正在开发一个作为设备所有者运行的应用程序,我想在其中构建一个自动更新程序.
为此,我使用PackageInstaller,因为我拥有使用它的权限,因为我的设备所有者位置.
private void installPackage(InputStream inputStream)
throws IOException {
notifyLog("Inizio aggiornamento...");
PackageInstaller packageInstaller = context.getPackageManager().getPackageInstaller();
int sessionId = packageInstaller.createSession(new PackageInstaller
.SessionParams(PackageInstaller.SessionParams.MODE_FULL_INSTALL));
PackageInstaller.Session session = packageInstaller.openSession(sessionId);
long sizeBytes = 0;
OutputStream out = null;
out = session.openWrite("my_app_session", 0, sizeBytes);
int total = 0;
byte[] buffer = new byte[65536];
int c;
while ((c = inputStream.read(buffer)) != -1) {
total += c;
out.write(buffer, 0, c);
}
session.fsync(out);
inputStream.close();
out.close();
session.commit(createIntentSender(sessionId));
}
private IntentSender createIntentSender(int sessionId) {
PendingIntent pendingIntent = PendingIntent.getBroadcast(
context,
sessionId,
new …Run Code Online (Sandbox Code Playgroud) android android-pendingintent device-owner packageinstaller cosu