Android应用程序自我更新

non*_*ckh 12 android apk android-install-apk

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

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

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
Run Code Online (Sandbox Code Playgroud)

Ale*_*xei 17

如果更新的apk具有相同的包名称并使用相同的密钥签名,则您只需发送一个将调用默认安卓程序的intent.已安装的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);
Run Code Online (Sandbox Code Playgroud)

  • @lexmiir,您好,感谢您的答复,我按您所说的去做,现在我收到一个警告对话框,提示解析器错误-解析软件包时出现问题。有什么线索吗?:-) (2认同)