我在Android下载二进制文件问题 和以编程方式在Android上安装应用程序的帮助下做到了这一点 .
我想立即进行自动更新和自动安装.它是本地的,所以它是非市场应用.
这是我的代码:
public void Update(String apkurl){
try {
URL url = new URL(apkurl);
HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
String PATH = Environment.getExternalStorageDirectory() + "/download/";
File file = new File(PATH);
file.mkdirs();
File outputFile = new File(file, "app.apk");
FileOutputStream fos = new FileOutputStream(outputFile);
InputStream is = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
while ((len1 = is.read(buffer)) != -1) {
fos.write(buffer, 0, len1);
}
fos.close();
is.close();//till here, it works fine …
Run Code Online (Sandbox Code Playgroud) 是否可以在设备不 root 的情况下静默安装 apk 文件?Stackoverflow 中有关此主题的大多数帖子都有些过时,并且尚不清楚较新版本的 Android (Kit Kat) 是否支持此功能。