Reputation: 69
I want to add checking update function in android app
I know this question is asked already and post a lib on Github
How to allow users to check for the latest app version from inside the app?
but my cant i post on the Google Play Store due to some problem of copyright
So i cant use the method
what should i do?
Upvotes: 0
Views: 960
Reputation: 625
Your code to check update once in a day. only works when you open app once in a day. in onCreate() of your launcher activity.
SharedPreferences mSettings = PreferenceManager.getDefaultSharedPreferences
(Dashboard.this);
long lastUpdateTime = mSettings.getLong("lastUpdateTime", 0);
/* Should Activity Check for Updates Now? */
if ((lastUpdateTime + (24 * 60 * 60 * 1000)) < System.currentTimeMillis()) {
/* Save current timestamp for next Check*/
SharedPreferences.Editor editor = mSettings.edit();
lastUpdateTime = System.currentTimeMillis();
editor.putLong("lastUpdateTime", lastUpdateTime);
editor.commit();
/* Start Update by using asynctask that run in backGround eg. http://portal.aksuniversity.com:8089/utility/apkversion*/
String URL = "your app url in my case ;
AsyncRequest asyncRequestTime = new AsyncRequest(Dashboard.this, "GET", null,
null, 3);
asyncRequestTime.execute(URL);
}
the response you will get is json object. get jsonObject key of version code and match it with your app version code
int versionCode = context.getPackageManager()
.getPackageInfo(context.getPackageName(), 0).versionCode;
if (versionCode < jsonObject.getString(""VersionCode))
//if greater show dialog for app update and download apk with new url of your app which will give you apk
for download you can use
private String callApiDownload(String address) {
try {
URL url = new URL(address);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
if (Cookie.getCookie() != null)
urlConnection.addRequestProperty("Cookie", Cookie.getCookie());
if (urlConnection.getResponseCode() == 200) {
File file = new File(Environment.getExternalStorageDirectory() + "/" + folderName);
boolean fileExist = true;
if (!file.exists()) {
fileExist = file.mkdir();
}
if (fileExist) {
String FileName = Environment.getExternalStorageDirectory() + "/" + folderName + "/"
+ fileName;
InputStream inputStream = urlConnection.getInputStream();
FileOutputStream outputStream = new FileOutputStream(FileName);
int bytesRead;
byte[] buffer = new byte[4096];
int total = 0;
int contentLength = urlConnection.getContentLength();
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.CUPCAKE) {
publishProgress((int) ((total * 100) / contentLength));
}
}
outputStream.flush();
outputStream.close();
inputStream.close();
return fileName;
}
} else {
InputStream inputStream = new BufferedInputStream(urlConnection.getErrorStream());
return fileName = Connection.convertInputStreamToString(inputStream);
}
} catch (Exception e) {
Log.e("Error: ", e.getMessage());
}
return fileName;
}
on download complete you can use intent to open apk for installation which will prompmt user for new update apk installation.
The first code will call the api once a day when user open the app. This api will return the json object of your app version code and version name. You need to manually maintain it when you create new apk for user. Parse the json object. And get the version code. In your app check if api version code is greater than your app version code. And if it is than fire a new api which is the last code i have given that download the your apk from url(in server you have to place apk for specified urlby calling which your apk will downloaded) which is last code you need to run in backthread asynctak. I will create the source code more proper and notifie you.
Upvotes: 1