安卓应用自更新

标签 android apk android-install-apk

<分区>

Possible Duplicate:
Android: install .apk programmatically

我需要更新我的 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

最佳答案

如果更新后的 apk 具有相同的包名并使用相同的 key 签名,您只需发送一个将调用默认 android 安装程序的 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);

关于安卓应用自更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9045837/

相关文章:

java - 如何在后台安装/卸载Android应用程序?

java - 获取结帐按钮以导航到结帐页面

android - java.lang.NullPointerException : Attempt to read from null array 异常

java - View.toString() 读出含义

java - 重载 APK 并创建自定义应用程序

android - 从 Google Play 安装时将参数传递给应用

java - 类处理,重复代码

android-studio - Android Studio : Error while Installing APKs (Path confusion from Windows to Linux)

xamarin - 为什么包名称在 Xamarin.Android 中加倍?

android - 安装.apk