更新 Android 应用程序(无需访问 Google Play 商店)

69
我写了一个应用程序的Beta版本。它将通过网络提供下载(我不会发布到Play市场)。当新版本发布时,是否可以在没有访问Play市场的情况下更新此应用程序?

是的,这是可能的。除此之外,你尝试过什么?你希望它有多深入?自动更新下载和安装?还是打开浏览器下载和安装? - Eluvatar
谢谢你的回答,但是Blumer已经给了我一个带有代码示例的答案。我认为对于我的情况来说已经足够了。再次感谢! - IQW
@IQW 你能否展示一下你尝试过的代码,因为我也遇到了同样的问题。我需要从一个域名中检查我上传的apk是否有新版本。 - TheCoderGuy
5个回答

92
当然可以。但是,你需要构建一个机制让你的应用程序向服务器发送请求,查找是否有更新版本的应用程序,如果有,则下载并安装它。一旦确定需要下载更新,您可以使用类似于以下AsyncTask的内容来完成下载:
protected String doInBackground(String... sUrl) {
    String path = "/sdcard/YourApp.apk";
    try {
        URL url = new URL(sUrl[0]);
        URLConnection connection = url.openConnection();
        connection.connect();

        int fileLength = connection.getContentLength();

        // download the file
        InputStream input = new BufferedInputStream(url.openStream());
        OutputStream output = new FileOutputStream(path);

        byte data[] = new byte[1024];
        long total = 0;
        int count;
        while ((count = input.read(data)) != -1) {
            total += count;
            publishProgress((int) (total * 100 / fileLength));
            output.write(data, 0, count);
        }

        output.flush();
        output.close();
        input.close();
    } catch (Exception e) {
        Log.e("YourApp", "Well that didn't work out so well...");
        Log.e("YourApp", e.getMessage());
    }
    return path;
}

// begin the installation by opening the resulting file
@Override
protected void onPostExecute(String path) {
    Intent i = new Intent();
    i.setAction(Intent.ACTION_VIEW);
    i.setDataAndType(Uri.fromFile(new File(path)), "application/vnd.android.package-archive" );
    Log.d("Lofting", "About to install new .apk");
    this.context.startActivity(i);
}

1
问题: "publishProgress((int) (total * 100 / fileLength));" 会发生什么?这个方法将在哪里被调用,它是做什么的?提前致谢。 - KingAlex1985
在此之后,APK文件是否会自动删除? - stephanlindauer
1
如果您使用此解决方案,请考虑以下几点:1)是的,它必须是外部存储器 - 内部存储器不会在应用程序之间共享,并且对“安装应用程序”不可见;2)您不能使用内容提供程序,因为这不受“安装应用程序”的支持(需要file://...),3)您可能希望使用Environment.getExternalStorageDirectory()而不是硬编码路径。否则,此解决方案有效,谢谢! - johndodo
6
如果没有请求额外权限,我可以抑制用户提示吗? - Jaydev
1
对于 Android N,我不得不添加文件提供程序或严格模式:https://dev59.com/zVoT5IYBdhLWcg3wvRpk - JoKr
显示剩余6条评论

17

是的,这是可能的,以下大致是你可以做的:

  1. Get the current application versionCode

    PackageInfo packageInfo = getPackageManager().getPackageInfo(context.getPackageName(), 0);
    int curVersionCode = packageInfo.versionCode;
    
  2. Have a server where you host the apk file and create a simple plain file containing only one integer, which represents the latest application version code.

  3. When the app starts (or whenever you want to check for an update), retrieve the latest versionCode from the server (i.e via an HTTP request) and compare it with the current app version.

  4. If there is a new version, download the apk and install it (will prompt a dialog for the user).

编辑:

您可以使用@Blumer的代码进行此操作。


在我的应用程序中使用Blumer的代码。 应用程序已成功下载和安装,但当我再次打开时,它会弹出错误提示There was a problem parsing the package。 请在这里帮助我https://stackoverflow.com/questions/48313766/getting-error-pop-up-after-installation-of-android-app。 - Shambhu

7

尽管如此,需要注意的是用户必须在设置中启用“允许安装非市场应用程序/未知来源”选项。


在上传应用程序时,如果禁用了上述设置,你知道会发生什么吗? - chandimak
上传到哪里?市场吗? 这是设备上的设置,不在二进制文件或市场中。 这就是我的意思:http://www.tech-recipes.com/wp-content/uploads/Screenshot_2012-03-18-16-55-37.png - stephanlindauer
抱歉,打错了。我想说的是“更新”,而不是“上传”。因此,当应用程序有更新时,并且此时上述设置的状态为“禁用”,会发生什么情况?它会自动允许更新,还是用户需要再次显式地将上述设置“启用”? - chandimak
1
据我所记,您可以下载APK文件,但无法安装。但是最好检查当前的情况。我认为谷歌想要完全禁止这些自发更新。 - stephanlindauer
在Oreo上(可能也适用于旧版本),如果您没有设置该权限,操作系统会告诉您需要进行设置,并带您进入相应的设置。您可以从那里配置可以从未知来源安装的应用程序列表 - 用户需要设置您的应用程序以允许此操作。 - Dan Harris

3
FYI,我刚在http://blog.vivekpanyam.com/evolve-seamlessly-deploy-android-apps-to-users/?hn上读到了这个内容。Evolve是为Android开发者设计的一个库,它能让他们在不通过Google Play或要求用户下载更新的情况下部署新版本的应用程序。它通过使用反射和动态字节码生成来“欺骗”Android运行新代码。虽然它还处于alpha阶段,但似乎可以通过很多技巧实现。除了恶意软件外,我怀疑它是否值得使用。

3

官方支持/In-app更新Google Play核心库

  • In-app更新是Play Core库的一个功能,它会提示活跃用户更新您的应用程序。In-app更新功能支持运行Android 5.0(API级别21)或更高版本的设备,并要求您的应用程序使用Play Core库版本1.5.0或更高版本。此外,仅支持Android移动设备、Android平板电脑和Chrome OS设备的In-app更新。

以下是更新流程类型

  1. 灵活更新(示例屏幕)

enter image description here

  1. 即时更新(示例屏幕)

enter image description here

该句话的意思是:“

参考资料:-更多关于这个主题的内容


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接