-1

I want to show dialogue when new version is available.

I want to make a json file into my web server, and I will manually update my app version in json file. and my app will parse this json file and will notify users and showing dialogue box to update my app from playstore link by clicking Update button.

I don't want to make this with firebase.

michael_heath
  • 5,262
  • 2
  • 12
  • 22
Liton
  • 21
  • 6

1 Answers1

0
public class ForceUpdateAsync extends AsyncTask<String, String, JSONObject>{

private String latestVersion;
private String currentVersion;
private Context context;
public ForceUpdateAsync(String currentVersion, Context context){
    this.currentVersion = currentVersion;
    this.context = context;
}

@Override
protected JSONObject doInBackground(String... params) {

    try 
 {
         latestVersion = Jsoup.connect("https://play.google.com/store/apps/details?id="+context.getPackageName()+"&hl=en")
                .timeout(30000)
                .userAgent("Mozilla/5.0 (Windows; U; WindowsNT 5.1; en-US; rv1.8.1.6) Gecko/20070725 Firefox/2.0.0.6")
                .referrer("http://www.google.com")
                .get()
                .select("div[itemprop=softwareVersion]")
                .first()
                 .ownText();

    } catch (IOException e) {
        e.printStackTrace();
    }
    return new JSONObject();
}

@Override
protected void onPostExecute(JSONObject jsonObject) {
    if(latestVersion!=null){
        if(!currentVersion.equalsIgnoreCase(latestVersion)){
           // Toast.makeText(context,"update is available.",Toast.LENGTH_LONG).show();
            if(!(context instanceof SplashActivity)) {
                if(!((Activity)context).isFinishing()){
                    showForceUpdateDialog();
                }
            }
        }
    }
    super.onPostExecute(jsonObject);
}

public void showForceUpdateDialog(){
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(new ContextThemeWrapper(context,
            R.style.DialogDark));

    alertDialogBuilder.setTitle(context.getString(R.string.youAreNotUpdatedTitle));
    alertDialogBuilder.setMessage(context.getString(R.string.youAreNotUpdatedMessage) + " " + latestVersion + context.getString(R.string.youAreNotUpdatedMessage1));
    alertDialogBuilder.setCancelable(false);
    alertDialogBuilder.setPositiveButton(R.string.update, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + context.getPackageName())));
            dialog.cancel();
        }
    });
    alertDialogBuilder.show();
}
 }

after that in your splash activity just use this code

 public void forceUpdate()
{
    PackageManager packageManager = this.getPackageManager();
    PackageInfo packageInfo = null;
    try {
        packageInfo =  packageManager.getPackageInfo(getPackageName(),0);
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    String currentVersion = packageInfo.versionName;
    new ForceUpdateAsync(currentVersion,BaseActivity.this).execute();
}
Archu Mohan
  • 199
  • 1
  • 5
  • 14
  • it's not working. i'm created ForceUpdateAsync.java and insert the above code, and i was put this below code to mainactivity. all string are created, but it still not showing the update dialogue. need to do anything on Minifests file? please help. – Liton Sep 19 '18 at 12:59
  • my main activity contain with 3 different fragment. where actually need to place this 2nd code? i'm placed this to main activity. – Liton Sep 19 '18 at 13:13
  • just add this in splash activity and make them in if contidition – Archu Mohan Sep 19 '18 at 13:18
  • not working bro... can you tell me clearly about how to do this step by step in short? – Liton Sep 19 '18 at 13:32
  • do i need to implements? and how to? – Liton Sep 19 '18 at 13:42