繁体   English   中英

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

[英]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

我试图在运行时检查是否有来自网址的应用程序的新版本。 我已经在在线域中部署了应用程序,就像这样www.test.com/androidapp/app_debug.apk ,这会自动下载我的应用程序。 我所要做的就是在这个环节检查,如果versionName此apk的是与安装了一个相同的,如果没有,那么我将展示一个Dialog ,这将给我用新的消息versionName ,将有两个按钮Cancel && Update 我知道如何做Dialog但我不知道如何在Url和我的apk之间实现这种通信。

我从SO的答案中尝试了一些代码,但直到现在还没有我所说的除外。 这是我尝试过的链接。

回答另一个问题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();

您应该在域上设​​置应用程序的最新版本名称,例如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();
    }

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
                         }


}});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM