Tuesday 30 January 2018

android programmatically update application when a new version is available

add gradle file in android .

    compile 'com.android.volley:volley:1.0.0'



public void getversionNumber(){
        StringRequest stringRequest = new StringRequest(Request.Method.GET,"https://play.google.com/store/apps/details?id=com.mwt.locobuddy&hl=en",
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
//                        LoadingDialog.cancelLoading();
                        String retur;
                        String parts[] = response.toString().split("softwareVersion");

                        retur= parts[1].substring(
                                parts[1].indexOf('>') + 1, parts[1].indexOf('<')
                        ).trim();

                        if (response != null && !response.equals("")) {
                            PackageManager pm = Activity_User.this.getPackageManager();
                            PackageInfo pInfo = null;

                            try {
                                pInfo =  pm.getPackageInfo(Activity_User.this.getPackageName(),0);

                            } catch (PackageManager.NameNotFoundException e1) {
                                e1.printStackTrace();
                            }
                            String currentVersion = pInfo.versionName;

                            if(!currentVersion.equalsIgnoreCase(retur)){
                                try {

                                    final AlertDialog.Builder builder = new AlertDialog.Builder(Activity_User.this);
                                    builder.setTitle("App Update Available");
                                    builder.setMessage("The newest version of the Sportobuddy app is now available. Download it and take your game to the next level."+"\n\n-Version : "+retur);
                                    builder.setPositiveButton("UPDATE", new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            //Click button action
                                            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(Constants.APP_URL)));
                                            dialog.dismiss();
                                        }
                                    });

                                    builder.setNegativeButton("NOT NOW", new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            //Cancel button action
                                        }
                                    });

                                    builder.setCancelable(false);
                                    builder.show();
                                }catch (Exception d){}

                            }
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        LoadingDialog.cancelLoading();
                        Toast.makeText(Activity_User.this,error.toString(),Toast.LENGTH_LONG).show();
                    }
                }) {
            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<String, String>();
                return params;
            }

        };
        stringRequest.setRetryPolicy(new DefaultRetryPolicy(
                30000,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        com.android.volley.RequestQueue requestQueue = Volley.newRequestQueue(Activity_User.this);
        requestQueue.add(stringRequest);
    }

No comments:

Post a Comment