Last active
February 4, 2016 07:51
-
-
Save samigehi/f2a8a90099970d935798 to your computer and use it in GitHub Desktop.
Simple class that check for update available or not on Play Store
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.sumeet; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import java.net.HttpURLConnection; | |
import java.net.URL; | |
import android.content.Context; | |
import android.os.AsyncTask; | |
import android.util.Log; | |
/** | |
* Simple class that check for update available or not on Play Store | |
* | |
* @author sumeet.gehi | |
*/ | |
public class CheckForUpdate { | |
static final String PLAY_STORE = "https://play.google.com/store/apps/details?id="; | |
static final String PLAY_STORE_TAG = "itemprop=\"softwareVersion\">"; | |
static final String MARKET_URL = "market://details?id="; | |
String TAG = CheckForUpdate.class.getSimpleName(); | |
String pkgName, version = "0"; | |
UpdateListner listner; | |
public CheckForUpdate(Context context) { | |
pkgName = context.getPackageName(); | |
try { | |
version = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName; | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
public void check(UpdateListner listner) { | |
setUpdateListner(listner); | |
new checkUpdate().execute(pkgName); | |
} | |
private class checkUpdate extends AsyncTask<String, Integer, String> { | |
@Override | |
protected String doInBackground(String... params) { | |
URL Url; | |
HttpURLConnection con; | |
try { | |
Url = new URL(PLAY_STORE.concat(pkgName)); | |
con = (HttpURLConnection) Url.openConnection(); | |
con.setDoInput(true); | |
String result = convert(con.getInputStream()); | |
Log.d("New Version is", result); | |
return result.trim(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return null; | |
} | |
@Override | |
protected void onPostExecute(String result) { | |
super.onPostExecute(result); | |
if (result != null) { | |
if (listner != null) { | |
if (!version.trim().equals(result)) | |
listner.OnUpdate(true, result); | |
else | |
listner.OnUpdate(false, result); | |
} | |
} else { | |
if (listner != null) { | |
listner.OnUpdate(false, null); | |
} | |
} | |
} | |
} | |
private String convert(InputStream inputStream2) throws IOException { | |
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream2)); | |
String line = ""; | |
while ((line = bufferedReader.readLine()) != null) { | |
if (line.contains(PLAY_STORE_TAG)) { | |
String[] split = line.split(PLAY_STORE_TAG); | |
String[] version = split[1].split("(<)"); | |
Log.d(TAG, "**** old ***" + this.version + "*********** " + version[0] + " *******************"); | |
inputStream2.close(); | |
// break; | |
return version[0]; | |
} | |
} | |
inputStream2.close(); | |
// Log.d("Result", line); | |
return null; | |
} | |
public void setUpdateListner(UpdateListner listner) { | |
this.listner = listner; | |
} | |
public interface UpdateListner { | |
public void OnUpdate(boolean isUpdate, String version); | |
} | |
} | |
and usage is as follow | |
CheckForUpdate update = new CheckForUpdate(getActivity()); | |
update.check(new CheckForUpdate.UpdateListner() { | |
@Override | |
public void OnUpdate(boolean isUpdate, String version) { | |
if (version != null) { | |
Toast.makeText(getContext(), "is update available " + isUpdate + ", version " + version, | |
Toast.LENGTH_SHORT).show(); | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment