-2

how can i force user to update my app by going to Google play store? if there's a new update a dialog will show which will have 2 buttons either update app or exit app.

Wont allow app to run unless latest version.

I am using eclipse and i cant migrate to android studio because of some project issues .

please help

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
SMD
  • 27
  • 1
  • 5
  • what is the problem ? – Amir Hossein Mirzaei Jun 12 '18 at 09:32
  • how can i implement? – SMD Jun 12 '18 at 09:34
  • Very broad, you need to try something before posting. I'd recommend looking at https://github.com/hummatli/AndroidAppUpdater where you have to provide your own version file hosted somewhere for your app to check. Do not use the play store method provided in this library, that is known to have problems. – CodeChimp Jun 12 '18 at 09:34
  • you can have a web api wich checks the running app version code with the newly publish app version and shows a dialog – Amir Hossein Mirzaei Jun 12 '18 at 09:35
  • please provide me a link...i am very new to programming. – SMD Jun 12 '18 at 09:36
  • 1
    On a side note, please definitely take the time to migrate to Android Studio when you can, even if it's problematic at first. Eclipse support has been discontinued for over 2 years now so you're missing out on a lot of new features. – Michael Dodd Jun 12 '18 at 09:43
  • You might want to edit the title of your question to be in **question** form - preferably ending with a question mark. People must have an understanding of what you're asking from the title alone. – Subaru Tashiro Jun 12 '18 at 10:00
  • 1
    Forcing someone to do something they don't want is already a terrible concept. – Phantômaxx Jun 12 '18 at 10:15

4 Answers4

0

Use Dialogs when your main Activity starts. Just redirect the user to the URL of your app on the PlayStore if he accepts, and exit the app (here are examples on how doing it).

Took from Anrdoid documentation :

public class YourDialogFragment extends DialogFragment {
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the Builder class for convenient dialog construction
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setMessage(R.string.please_update)
               .setPositiveButton(R.string.Ok, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       // launch a browser with your PlayStore APP URL
                   }
               })
               .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       // Exit the app
                       android.os.Process.killProcess(android.os.Process.myPid()); 
                   }
               });
        // Create the AlertDialog object and return it
        return builder.create();
    }
}

Now, when you create an instance of this class and call show() on that object, the dialog appears as shown in figure 1.

Just create an instance of it and use the show() method in your onCreateDialog from your MainActivity.

Note that Dialogs uses Fragments, which requieres API level 11. (You should be able to check the API level you're building to in your build.gradle file)

Nark
  • 454
  • 1
  • 7
  • 18
0

Implement a way for the app to check if it is the latest version or not.

You can do this by hosting an update file that contains information on what the latest version is. This file is commonly in json format but can also be in any format you prefer. The app would have to query this update file and if the current version of the app is less than the version indicated in the update file, then it would show the update prompt.

If the app determines that an update is needed, open a dialog prompt then open the app's play store page

To launch a dialog prompt refer to the official Dialogs guide. Given that the question is not "how to launch a dialog" I will focus on discussing how to update the app.

To open google play store to a particular app page you must launch an intent with the View action and Market scheme uri like so

startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));

Be wary that if the device does not have google play store installed, then this will throw an exception. It is also possible for other apps to receive this type of intent and in the case where multiple apps can receive the intent, an app picker dialog will appear.

Challenges:

If the app must check for updates and can only run if it is the latest version, then the app cannot run if the device is not connected to the internet.

The app will have a hard dependency on google play store and cannot run if an update is needed and there is no play store on the device

If the update file is unavailable for any reason then the app will not run as well

Subaru Tashiro
  • 2,166
  • 1
  • 14
  • 29
0

Use dialogs as mentioned by Noafix. Call an alert dialog when your version mismatches.

Also set dialog cancelable to false so that user cant remove dialog by pressing back!

 private void showDialog()
    {
        final android.app.AlertDialog.Builder alertDialogBuilder = new android.app.AlertDialog.Builder(this);
        alertDialogBuilder.setTitle("Update");
        alertDialogBuilder.setMessage("Please update to continue?");
        alertDialogBuilder.setIcon(R.drawable.warning);
        alertDialogBuilder.setCancelable(false)
        alertDialogBuilder.setPositiveButton("Confirm",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                           startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName)));
                    }
                });

        // Add a negative button and it's action. In our case, just hide the dialog box
        alertDialogBuilder.setNegativeButton("Cancel",
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                     finishAffinity();
                    }
                });

        // Now, create the Dialog and show it.
        final android.app.AlertDialog alertDialog = alertDialogBuilder.create();
        alertDialog.show();
    }

You could get your version name as:

versionName = getApplicationContext().getPackageManager().getPackageInfo(getApplicationContext().getPackageName(), 0).versionName;

Now get your current version from your server using any rest client http calls and check with your version:

 if(!versionName.equals(version)){showDialog();}

Note: For this you should implement a version file in server in which you must add new version in that file so that using http calls your app can get the new version name and check with app version!

Karthik CP
  • 1,150
  • 13
  • 24
0
  • You absolutely need the users to update to continue using the app, you could provide a simple versioning API. The API would look like this:

    versionCheck API:

    ->Request parameters:

    int appVersion

    -> Response

    boolean forceUpgrade boolean recommendUpgrade

    When your app starts, you could call this API that pass in the current app version, and check the response of the versioning API call.

    If forceUpgrade is true, show a popup dialog with options to either let user quit the app, or go to Google Play Store to upgrade the app.

    Else if recommendUpgrade is true, show the pop-up dialog with options to update or to continue using the app.

    Even with this forced upgrade ability in place, you should continue to support older versions, unless absolutely needed.

    try this: First you need to make a request call to the playstore link, fetch current version from there and then compare it with your current version.

    String currentVersion, latestVersion; Dialog dialog;

    private void getCurrentVersion(){ PackageManager pm = this.getPackageManager(); PackageInfo pInfo = null;

        try {
            pInfo =  pm.getPackageInfo(this.getPackageName(),0);
    
        } catch (PackageManager.NameNotFoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        currentVersion = pInfo.versionName;
    

    new GetLatestVersion().execute();

    }

    private class GetLatestVersion extends AsyncTask {

        private ProgressDialog progressDialog;
    
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }
    
        @Override
        protected JSONObject doInBackground(String... params) {
            try {
    
                Document doc = Jsoup.connect(urlOfAppFromPlayStore).get();
                latestVersion = doc.getElementsByAttributeValue
                        ("itemprop","softwareVersion").first().text();
    
            }catch (Exception e){
                e.printStackTrace();
    
            }
    
            return new JSONObject();
        }
    
        @Override
        protected void onPostExecute(JSONObject jsonObject) {
            if(latestVersion!=null) {
                if (!currentVersion.equalsIgnoreCase(latestVersion)){
                   if(!isFinishing()){ 
                    showUpdateDialog();
                    }
                }
            }
            else
                background.start();
            super.onPostExecute(jsonObject);
        }
    }
    

    private void showUpdateDialog(){

        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
    
        builder.setTitle("A New Update is Available");
    
        builder.setPositiveButton("Update", new 
       DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse
                        ("market://details?id=yourAppPackageName")));
                dialog.dismiss();
            }
        });
    
        builder.setNegativeButton("Cancel", new 
          DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                background.start();
            }
        });
    
        builder.setCancelable(false);
        dialog = builder.show();
    }