更新Android应用(不使用Google Play)

IQW*_*IQW 55 android

我写了一个应用程序的Beta版本.它可以通过网络下载(我不会将其发布到Play Market).在新版本发布时,是否可以在没有Play Market的情况下更新此应用程序?

Blu*_*mer 80

绝对.但是,您需要构建一个机制,让您的应用程序调用服务器的主页,查看是否有更新版本的应用程序,如果有,请将其下拉并安装.一旦确定需要下拉更新,就可以使用与此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);
}
Run Code Online (Sandbox Code Playgroud)

  • 如果没有寻求额外的权限,我可以取消用户提示吗? (5认同)

iTe*_*ech 15

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

  1. 获取当前的应用程序版本代码

    PackageInfo packageInfo = getPackageManager().getPackageInfo(context.getPackageName(), 0);
    int curVersionCode = packageInfo.versionCode;
    
    Run Code Online (Sandbox Code Playgroud)
  2. 有一个托管apk文件的服务器,并创建一个只包含一个整数的简单普通文件,该文件代表最新的应用程序版本代码.

  3. 当应用程序启动时(或者每当您想要检查更新时),从服务器检索最新的versionCode(即通过HTTP请求)并将其与当前的应用程序版本进行比较.

  4. 如果有新版本,请下载apk并安装它(将为用户提示对话框).

编辑:

您可以使用@Blumer的代码.


ste*_*uer 6

但是,请记住,用户必须在其设置中启用"允许安装非市场应用程序/未知来源".


Pra*_*ags 3

应用内更新的官方支持/库 Google Play 核心

  • 应用内更新是Play Core 库的一项功能,可提示活跃用户更新您的应用。运行 Android 5.0(API 级别 21)或更高版本的设备支持应用内更新功能,并且需要您的应用使用Play Core 库版本 1.5.0 或更高版本。此外,仅 Android 移动设备、Android 平板电脑和 Chrome 操作系统设备支持应用内更新。

以下是更新流程的类型

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

在此输入图像描述

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

在此输入图像描述

参考:-有关此主题的更多信息