【问题标题】:How to check from a web if the versionName of apk with the installed apk at device it is the same or not in Android Studio如何从网络检查 apk 的 versionName 是否与设备上安装的 apk 在 Android Studio 中是否相同
【发布时间】:2019-08-30 10:37:23
【问题描述】:

我正在尝试在运行时检查是否有来自 url 的应用程序的新版本。 我已经在类似www.test.com/androidapp/app_debug.apk 的在线域中部署了该应用程序,这会自动下载我的应用程序。 我要做的是检查此链接,如果此 apk 的 versionName 与已安装的相同,否则我将显示 Dialog,它将给我一条带有新 versionName 的消息和将有两个按钮Cancel && Update。 我知道如何进行Dialog,但我不知道如何在Url 和我的apk 之间实现这种通信。

我已经从SO 的答案中尝试了一些代码,但直到现在还不是我想要的。 这是我尝试过的链接。

Answer from another question SO

这是我到目前为止所尝试的取决于@BryanIbrahim 的回答

我在这里获取应用程序的当前版本。

String getVersionFromUrl = "http://test.com/AndroidApp/text.txt";
//At text.txt I have only test.v1.0.2

 URL u = null;

        try {
            u = new URL(path);
            HttpURLConnection c = (HttpURLConnection) u.openConnection();
            c.setRequestMethod("GET");
            c.connect();
            InputStream in = c.getInputStream();
            final ByteArrayOutputStream bo = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            in.read(buffer); // Read from Buffer.
            bo.write(buffer); // Write Into Buffer.
            String getVersion = bo.toString().substring(12, 17);
            String getVersionFromUrl = BuildConfig.VERSION_NAME;
            if (!getVersionFromUrl.equals(getVersion))  {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        AlertDialog.Builder builder1 = new AlertDialog.Builder(context);
                        builder1.setMessage("It is a new version of this app");
                        builder1.setCancelable(true);
                        builder1.setPositiveButton(
                                "Yes",
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int id) {

                                        dialog.cancel();
                                    }
                                });

                        builder1.setNegativeButton(
                                "No",
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int id) {
                                        dialog.cancel();
                                    }
                                });

                        builder1.show();

                    }
                });

            }

        } catch (Exception e) {
            Log.e("YourApp", "Well that didn't work out so well...");
            Log.e("YourApp", e.getMessage());

        }
        return path;
    }

    @Override
    protected void onPostExecute(String path) {
        Intent i = new Intent();
        String path2 = "http://test.com/AndroidApp/testv1.0.2.apk";
        i.setAction(Intent.ACTION_VIEW);
        i.setDataAndType(Uri.fromFile(new File(path2)), "application/vnd.android.package-archive" );
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
       // mContext.startActivity(i);
    }
}

onResume() 方法中,我调用了这样的方法。

getVersionName gTV = new getVersionName(getApplicationContext());
gTV.execute();

【问题讨论】:

    标签: android jsoup androidhttpclient


    【解决方案1】:

    Jsoup 不适合您使用的目的。 最好将 json 文件上传到服务器并使用okhttp 获取它。 示例:www.oye.com/update.json 您可以在其中设置版本信息、新增功能、URL 和其他信息。 否则你会在使用 jsoup 来检查你的应用更新时体验不好

    答案已更新 您服务器上的 JsonFile(示例:www.oye.com/update.json)

    {   "name":"AppName",   "size":"8mb",   "url":"https://www.youall.com/update.apk",  "version":"1.08" }
    

    使用okhttp

    OkHttpClient client=new OkHttpClient();
    
    
    Request request=new Request.Builder().url("www.oye.com/update.json").build();
    
            Call call = client.newCall(request);
            call.enqueue(new Callback() {
                    public void onResponse(Call call, final Response response) 
                    throws IOException
                    {
    
    JSONObject obj=new JSONObject(reponse.body().string);
                             if(obj.getDouble("version")>yourAppvrrsion){
                                 //update avialable
                             }else{
                                 //not avialable
                             }
    
    
    }});
    

    【讨论】:

    • 我在那里创建了一个txt 文件,我写了类似v1.0.2 的内容,然后我正在使用HttpURLConnection c = (HttpURLConnection) u.openConnection(); c.setRequestMethod("GET"); c.connect(); InputStream in = c.getInputStream(); 阅读它直到现在它检查哪个版本存在,但问题是现在我不知道如何从 url 更新 apk。
    • 您的应用在 play/应用商店有售吗
    • 不,我首先尝试修复一些错误并查看是否可以从 Web 进行更新,然后我将上传到 playstore。
    • 我已更新问题,请查看更改。
    • 如何使该更新可供安装?
    【解决方案2】:

    您应该在域中设置您的应用程序的最新版本名称,例如 1.0.0,而不是简单地上传整个 apk。然后使用PackageManager检索versionName并与在线版本进行比较。

    PackageManager packageManager = getPackageManager();
        try {
            PackageInfo packageInfo = packageManager.getPackageInfo(getPackageName(), 0);
            String localVersionName = packageInfo.versionName;
    
           //Compare with the online version
          if(localVersionName  != onlineVersionName){
           //Update the app
           }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    

    【讨论】:

    • 如何从域中获取versionName 这怎么可能?您只添加了如何获取localVersionName ?
    • 如果你有一个数据库来设置和检索会更方便。你可以使用jsoup.org,一个java HTML解析器从元素中提取准确的版本名称
    • 我打算用jsoup.org 试试,你说如果我有一个数据库来设置和检索你的意思会更方便?
    猜你喜欢
    • 2022-01-15
    • 2014-11-07
    • 1970-01-01
    • 1970-01-01
    • 2018-01-06
    • 1970-01-01
    • 2011-12-07
    • 2017-01-31
    • 1970-01-01
    相关资源
    最近更新 更多