Android应用自更新 [英] Android application self-update

查看:20
本文介绍了Android应用自更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
Android:以编程方式安装 .apk

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

I need to update my android application. Internally the program, I download the new version. How can I replace the current version by that new was downloaded (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

推荐答案

如果更新后的 apk 具有相同的包名并使用相同的密钥进行签名,您只需发送一个 Intent,该 Intent 将调用默认的 android 安装程序.安装的 apk 将被覆盖.

If the updated apk has the same package name and is signed with the same key you can just send a intent which will call a default android installer. The installed apk will be overriden.

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

这篇关于Android应用自更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆