如何从下载的 apk 中实现应用程序自我更新?
How to implement app self-update from downloaded apk?
我正在使用应用程序的 OTA 更新。它是这样工作的:我点击 "check update",它会检查它,如果存在则下载并在设备上保存 apk。然后我可以安装它,当然我有一个确认对话框。我需要默默地完成并重新启动应用程序。
我想让它在下载完成后自动安装。所以我只需单击,如果有一些更新,应用程序会在新版本中重新启动。我不知道该怎么做。 设备已root。
出于安全原因,您不能拥有它。没有用户应用程序可以静默安装在非 root phone.
您唯一能做的就是告诉用户您刚刚下载了更新并询问 a/he 是否要安装它。如果是这样,则触发安装意图并让您的用户继续安装。
以下代码仅适用于 root 设备!
private int installApk(File file) {
if (!file.exists()) throw new IllegalArgumentException();
Process process = null;
try {
process = Runtime.getRuntime().exec(new String[]{"su", "-c", "pm install -r " + file
.getAbsolutePath()});
int exitCode = process.waitFor();
if (exitCode != 0) throw new RuntimeException();
} catch (IOException | InterruptedException exception) {
Log.w(getClass().getSimpleName(), exception);
}
return (process == null) ? Integer.MIN_VALUE : process.exitValue();
}
我正在使用应用程序的 OTA 更新。它是这样工作的:我点击 "check update",它会检查它,如果存在则下载并在设备上保存 apk。然后我可以安装它,当然我有一个确认对话框。我需要默默地完成并重新启动应用程序。
我想让它在下载完成后自动安装。所以我只需单击,如果有一些更新,应用程序会在新版本中重新启动。我不知道该怎么做。 设备已root。
出于安全原因,您不能拥有它。没有用户应用程序可以静默安装在非 root phone.
您唯一能做的就是告诉用户您刚刚下载了更新并询问 a/he 是否要安装它。如果是这样,则触发安装意图并让您的用户继续安装。
以下代码仅适用于 root 设备!
private int installApk(File file) {
if (!file.exists()) throw new IllegalArgumentException();
Process process = null;
try {
process = Runtime.getRuntime().exec(new String[]{"su", "-c", "pm install -r " + file
.getAbsolutePath()});
int exitCode = process.waitFor();
if (exitCode != 0) throw new RuntimeException();
} catch (IOException | InterruptedException exception) {
Log.w(getClass().getSimpleName(), exception);
}
return (process == null) ? Integer.MIN_VALUE : process.exitValue();
}