Android:以编程方式安装.apk我在我的帮助下做了这个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 - .apk is download to my sdcard in download file
Intent promptInstall = new Intent(Intent.ACTION_VIEW)
.setData(Uri.parse(PATH+"app.apk"))
.setType("application/android.com.app");
startActivity(promptInstall);//installation is not working
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "Update error!", Toast.LENGTH_LONG).show();
}}我的权限是INTERNET, WRITE_EXTERNAL_STORAGE, INSTALL_PACKAGES,和DELETE_PACKAGES.当意图promptInstall,则应用程序崩溃=/那么,我是缺少权限,还是代码不正确,还是有更好的方法来做到这一点?
- 3 回答
- 0 关注
- 485 浏览
添加回答
举报
0/150
提交
取消