Basically, you should check the latest version of your application on the market and the version of the application on the device and decide if an update is available. To do this, try the following:
You should use this to get the current version (the version of the application on the device):
private String getCurrentVersion(){ PackageManager pm = this.getPackageManager(); PackageInfo pInfo = null; try { pInfo = pm.getPackageInfo(this.getPackageName(),0); } catch (PackageManager.NameNotFoundException e1) { e1.printStackTrace(); } String currentVersion = pInfo.versionName; return currentVersion; }
And use this to get the latest version in a google game (taken from https://stackoverflow.com/a/127263/... ):
private class GetLatestVersion extends AsyncTask<String, String, String> { String latestVersion; @Override protected void onPreExecute() { super.onPreExecute(); } @Override protected String doInBackground(String... params) { try {
Then, when your application starts, check them against each other as follows:
String latestVersion = ""; String currentVersion = getCurrentVersion(); Log.d(LOG_TAG, "Current version = " + currentVersion); try { latestVersion = new GetLatestVersion().execute().get(); Log.d(LOG_TAG, "Latest version = " + latestVersion); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } //If the versions are not the same if(!currentVersion.equals(latestVersion)){ final AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("An Update is Available"); 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("market://details?id=your app package address"))); dialog.dismiss(); } }); builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //Cancel button action } }); builder.setCancelable(false); builder.show(); }
And show the user update dialog box. But make sure you have already imported the jsoup library.
Optional: to import the jsoup library, follow these steps:
1- go to the "File" menu
2- design structure
3 - left side click on the application
4- Select the dependencies tab
5- Click + 6 - Click the library dependency link
7- Search for "jsoup"
8 - select org.jsoup: jsoup and click ok
Mohammad Nov 25 '15 at 19:36 2015-11-25 19:36
source share