如何从web检查如果在设备上安装了apk的apk的versionName在Android Studio中是否相同

问题描述 投票:0回答:2

我试图在运行时检查是否有来自网址的应用程序的新版本。我已经在在线域上部署了应用程序,就像这个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
2个回答
0
投票

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
                         }


}});

0
投票

您应该在域上设​​置应用程序的最新版本名称,例如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();
    }
© www.soinside.com 2019 - 2024. All rights reserved.