Update Script


this can be used by adding this to you splash screen or anyother of your activitys

if (mWifi.isConnected()) {
Intent i3 = new Intent(splashscreen.this, Update.class);
startService(i3);
}

 

this is what checks for the update.

package com.lukemovement.roottoolbox.pro;

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 java.net.URLConnection;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.util.Log;

/**
* @author rac
*
*/
public class Update extends Service {

private static final String EXTRA_NOTIFY = “EXTRA_NOTIFY”;
private static final String DOWNLOAD_URL = “DOWNLOAD_URL”;
/** Constants **/
private static String urlLatestVersion = “http://dl.dropbox.com/u/22618820/open%20source/Toolbox/update/latest_version.txt”;
private static String urlLatestUrl = “http://dl.dropbox.com/u/22618820/open%20source/Toolbox/update/latest_url_pro.txt”;
private static String logName = “App update”;
private static int UPDATE_NOTIFICATION_ID = 1;

/** Variables **/
private int latestVersion = 0;
private int actualVersion = 0;
private String latestUrl = “”;

/** managers **/
@SuppressWarnings(“unused”)
private static SharedPreferences settings;

@Override
public void onStart(Intent intent, int startId) {
// check if the intent contains notification data 🙂
if (intent.getBooleanExtra(EXTRA_NOTIFY, false) && intent.hasExtra(DOWNLOAD_URL)) {
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

notificationManager.cancel(UPDATE_NOTIFICATION_ID);

//launching the browser
Intent downloadIntent = new Intent(             android.content.Intent.ACTION_VIEW      , Uri.parse(intent.getStringExtra(DOWNLOAD_URL)));
downloadIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
this.startActivity(downloadIntent);
this.stopSelf();
return;
}
/** get me my preferences **/
settings = PreferenceManager
.getDefaultSharedPreferences(getBaseContext());
/** which version am I running? **/
try {
PackageInfo pi = getPackageManager().getPackageInfo(
getPackageName(), 0);
actualVersion = pi.versionCode;
} catch (PackageManager.NameNotFoundException e) {
}
/** does a update exist? **/
if (checkForUpdate()) {
// Update exists
Log.i(logName, “App ” + latestVersion
+ ” is available!”);
updateNotify();
} else {
// no update needed
Log.i(logName,
“you have the latest version of App running”);
}
this.stopSelf();
}

/* (non-Javadoc)
* @see android.app.Service#onBind(android.content.Intent)
*/
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}

private boolean checkForUpdate() {
try {
latestVersion = Integer.parseInt(getHttpString(urlLatestVersion));
} catch (IOException e1) {
// TODO Auto-generated catch block
// e1.printStackTrace();
Log.i(logName, e1.getLocalizedMessage());
return false;
} catch (NumberFormatException e2) {
Log.i(logName, e2.getLocalizedMessage());
return false;
}

if (actualVersion < latestVersion) {
try {
latestUrl = getHttpString(urlLatestUrl);
} catch (IOException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
Log.i(logName, e.getLocalizedMessage());
}
return true;
} else
return false;
}

private void updateNotify() {
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
int icon = R.drawable.update;
long when = System.currentTimeMillis();
Notification notification = new Notification(icon,”App update is available”, when);
//Intent notifyIntent = new Intent(             android.content.Intent.ACTION_VIEW      , Uri.parse(latestUrl));
Intent notifyIntent = new Intent(this, Update.class);
notifyIntent.putExtra(Update.EXTRA_NOTIFY, true);
notifyIntent.putExtra(Update.DOWNLOAD_URL, latestUrl);

PendingIntent pendingIntent =  PendingIntent.getService(
this,
0,
notifyIntent,
android.content.Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);

notification.setLatestEventInfo(
getApplicationContext(),
“App update is available”, “click to download”,
pendingIntent);

notificationManager.notify(UPDATE_NOTIFICATION_ID, notification);

}

/**
* taken from http://www.devx.com/wireless/Article/39810/1954
*
* @param urlString
* @return InputStream
* @throws IOException
*/
private String getHttpString(String urlString) throws IOException {
InputStream in = null;
int response = -1;

URL url = new URL(urlString);
URLConnection conn = url.openConnection();

if (!(conn instanceof HttpURLConnection))
throw new IOException(“Not an HTTP connection”);

try {
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod(“GET”);
httpConn.connect();

response = httpConn.getResponseCode();
if (response == HttpURLConnection.HTTP_OK) {
in = httpConn.getInputStream();
}
} catch (Exception ex) {
throw new IOException(“Error connecting”);
}
if (in != null) {
StringBuilder sb = new StringBuilder();
String line;

try {
BufferedReader reader = new BufferedReader(
new InputStreamReader(in, “UTF-8”));
while ((line = reader.readLine()) != null) {
sb.append(line); // .append(“\n”);
}
} finally {
in.close();
}
return sb.toString();
} else
return “”;

}

}

the files mention at the top need to consist of a market link and the app version. enter them into a browser to veiw examples.

E.g market://details?id=com.lukemovement.roottoolbox.pro

E.g 16

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: