【问题标题】:Android application self-update [duplicate]Android应用程序自我更新[重复]
【发布时间】:2012-01-28 14:06:59
【问题描述】:

可能重复:
Android: install .apk programmatically

我需要更新我的安卓应用程序。在程序内部,我下载了新版本。如何用下载的新版本(以编程方式)替换当前版本?

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();

    ....     here I have the file of new app, now I need use it

【问题讨论】:

    标签: android apk android-install-apk


    【解决方案1】:

    如果更新后的 apk 具有相同的包名称并使用相同的密钥进行签名,您可以发送一个调用默认 android 安装程序的意图。已安装的 apk 将被覆盖。

    Intent intent = new Intent(Intent.ACTION_VIEW);
    Uri uri = Uri.fromFile(new File(pathToApk));
    intent.setDataAndType(uri, "application/vnd.android.package-archive");
    startActivity(intent);
    

    【讨论】:

    • 嗨@lexmiir,谢谢你的回复,我照你说的做了,现在我收到一个警告对话框,提示解析器错误 - 解析包时出现问题。有什么线索吗? :-)
    • 嗨@nonickh,您是否使用相同的密钥签署了两个apk?包名相同的应用使用不同的key签名时可能会出现这个错误
    • 您好@lexmiir,抱歉耽搁了,我比较了两个文件,结果不一样,可能是复制网站的过程造成的,我先尝试解决这个问题,感谢您的支持
    • 可能已经解决了,但是为了将来参考你不应该写完整的缓冲区,只有读取的字节数,即fos.write(buff, 0, len),其中len可能会小于buffer.length,当最后一个块时写好了。
    • 我遇到了同样的错误,通过在ftp程序中使用二进制模式上传apk文件解决了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-12
    • 1970-01-01
    • 1970-01-01
    • 2015-02-11
    • 1970-01-01
    • 1970-01-01
    • 2011-05-15
    相关资源
    最近更新 更多