Android software updates the app when a new version is available

In my application, I want to check if there is an updated version of my application in the application store. If there is, then it is necessary to inform the user through a warning message, and if he / she chooses the update, I want to update the new version. I want to do all this through my application. Is it possible?

+8
source share
8 answers

I have the same problem, but it is solved by the JSOUP library. Here is the link to download the library: http://jsoup.org/download

String newVersion = Jsoup .connect( "https://play.google.com/store/apps/details?id=" + "Package Name" + "&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(); Log.e("new Version", newVersion); 
+13
source

Google does not provide an API for this.

However, you can make an HTTP request in the web version of the game store ( https://play.google.com/store/apps/details?id=your.namespace )

To make a request, you can use DefaultHttpClient

Once you get the content of the page, you should analyze it ( jsoup is a good solution) and search:

 <div class="content" itemprop="softwareVersion"> 2.2.0 </div> 

Once you find this part of the page, you can extract the version number and compare it with the one available in your application:

 try { String version = this.getPackageManager().getPackageInfo(this.getPackageName(), 0).versionName; if( ! version.equals(versionFromHTML)) { Toast.makeText(this, "New version available on play store", Toast.LENGTH_SHORT); } } catch (NameNotFoundException e) { //No version do something } 

For the HTML parsing part, look here.

Keep in mind that everyone will not see the new version at the same time. This may take some time for distribution (possibly due to cache).

+6
source

EDIT

They recently changed the Google Play website, and now this code is broken. Avoid this decision or be prepared to fix your application when Google Play pages change.

Ahmad Arlan's answer is the best answer. But if you got here and you try to cut and paste its code, you will go through the same problems as me, and I could just post it here to help others like me.


  • Make sure you have INTERNET permission for your AndroidManifest.xml file.

     <uses-permission android:name="android.permission.INTERNET"/> 
  • Add JSOUP to your build.gradle module.

     dependencies { compile 'org.jsoup:jsoup:1.10.2' } 
  • Surround the try and catch snippet and not run it in the main thread.

     public class MainActivity extends AppCompatActivity { String newVersion; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); new FetchAppVersionFromGooglePlayStore().execute(); } class FetchAppVersionFromGooglePlayStore extends AsyncTask<String, Void, String> { protected String doInBackground(String... urls) { try { return Jsoup.connect("https://play.google.com/store/apps/details?id=" + "com.directed.android.smartstart" + "&hl=en") .timeout(10000) .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 (Exception e) { return ""; } } protected void onPostExecute(String string) { newVersion = string; Log.d("new Version", newVersion); } } } 

I posted a copy of the github project here.

+2
source

It worked for me. Add this dependency.

  implementation 'org.jsoup:jsoup:1.8.3' 

In the onCreate () method, use the following code:

 try { String currentVersion=""; currentVersion = getApplicationContext().getPackageManager().getPackageInfo(getPackageName(), 0).versionName; Log.e("Current Version","::"+currentVersion); } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } new GetVersionCode().execute(); 

Create class GetVersionCode:

 private class GetVersionCode extends AsyncTask<Void, String, String> { @Override protected String doInBackground(Void... voids) { String newVersion = null; try { Document document = 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(); if (document != null) { Elements element = document.getElementsContainingOwnText("Current Version"); for (Element ele : element) { if (ele.siblingElements() != null) { Elements sibElemets = ele.siblingElements(); for (Element sibElemet : sibElemets) { newVersion = sibElemet.text(); } } } } } catch (IOException e) { e.printStackTrace(); } return newVersion; } @Override protected void onPostExecute(String onlineVersion) { super.onPostExecute(onlineVersion); if (onlineVersion != null && !onlineVersion.isEmpty()) { if (onlineVersion.equals(currentVersion)) { } else { AlertDialog alertDialog = new AlertDialog.Builder(context).create(); alertDialog.setTitle("Update"); alertDialog.setIcon(R.mipmap.ic_launcher); alertDialog.setMessage("New Update is available"); alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Update", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { try { startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + context.getPackageName()))); } catch (android.content.ActivityNotFoundException anfe) { startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + context.getPackageName()))); } } }); alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); alertDialog.show(); } } Log.d("update", "Current version " + currentVersion + "playstore version " + onlineVersion); } } 
+1
source

Well, there is another way that I understood, and that is how I do it.

 HttpPost httppostUserName = new HttpPost("https://androidquery.appspot.com/api/market?app=com.supercell.clashofclans"); //your package name HttpClient httpclient = new DefaultHttpClient(); HttpResponse responseUser = httpclient.execute(httppostUserName); String responseStringUser = EntityUtils.toString(responseUser.getEntity(), HTTP.UTF_8); Log.d(Constants.TAG, "Response: " + responseStringUser); try { JSONObject Json = new JSONObject(responseStringUser); newVersion = Json.getString("version"); } catch (Exception e) { e.printStackTrace(); } 

You will get a clearer view if you paste the URL into your browser to see the results.

0
source

Google Play is already doing this. When you download a new version of your application, it will either send a warning directly to your users ’devices or will automatically download the update if the user has enabled this option in the Google Play application.

0
source
  private void checkForUpdate() { PackageInfo packageInfo = null; try { packageInfo=getPackageManager().getPackageInfo(DashboardActivity.this.getPackageName(), 0); int curVersionCode = packageInfo.versionCode if (curVersionCode > 1) { // instead of one use value get from server for the new update. showUpdateDialog(); } } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } } 
0
source

You can use updates to the built-in Play Core library to solve this problem. You can check for updates and install them if they are available without a problem.

Please note that updates in the application only work with devices running Android 5.0 (API level 21) or higher and require the use of the Play Core library 1.5.0 or higher.

Updates in the application are not compatible with applications that use APK extension files (.obb files). You can choose flexible downloads or immediate updates that Google Play will take care of downloading and installing the update for you.

 dependencies { implementation 'com.google.android.play:core:1.5.0' ... } 
0
source

All Articles