yijidemomo 2012-09-26 05:33 采纳率: 100%
浏览 4198
已采纳

用程序实现安装.apk,引用程序无法运行

我想要实现自动更新之后马上自动安装,它是本地的所以它不是在应用市场里的。
这是我的代码

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();
           Intent promptInstall= new Intent(Intent.ACTION_VIEW)
           .setData(Uri.parse(PATH+"app.apk"))
           .setType("application/android.com.app");
            startActivity(promptInstall);
       } catch (IOException e) {
           Toast.makeText(getApplicationContext(), "Update error!", Toast.LENGTH_LONG).show();
       }
 }  

我的权限是INTERNET, WRITE_EXTERNAL_STORAGE, INSTALL_PACKAGES, DELETE_PACKAGES
当网络提示安装加载的时候,引用程序就无法继续运行
所以,是我缺少什么权限还是我的代码不正确,或者有更好的方法来实现吗?

  • 写回答

2条回答

  • yijidemomo 2012-09-26 06:06
    关注

    我已经解决了这个问题了,在setdata和settype中有错误

    Intent intent= new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/download/" + "app.apk")), "application/vnd.android.package-archive");
    startActivity(intent);
    

    现在正确了,我的自动更新也可以了。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 matlab计算中误差
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊