package dk.thoerup.checkupdates; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.content.DialogInterface.OnClickListener; import android.content.SharedPreferences.Editor; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.net.Uri; import android.os.AsyncTask; import android.util.Log; public class CheckUpdates { public static long TIMESPAN_DAY = 24*60*60*1000; // one day final static String PREFS = "CheckUpdate"; int versionCode; String versionName; String packageName; String phone_model; String androidVersion; String title; Context context; public void checkForUpdates(Context context, String url, String title) { this.context = context; this.title = title; SharedPreferences prefs = context.getSharedPreferences(PREFS, Context.MODE_PRIVATE); long lastCheck = prefs.getLong("lastcheck", 0); long now = System.currentTimeMillis(); //should i use android.os.SystemClock.elapsedRealtime() instead ? if ( (now+TIMESPAN_DAY) > lastCheck ){ PackageManager pm = context.getPackageManager(); try { PackageInfo pi; pi = pm.getPackageInfo(context.getPackageName(), 0); versionCode = pi.versionCode; versionName = pi.versionName; packageName = pi.packageName; phone_model = android.os.Build.MODEL; androidVersion = android.os.Build.VERSION.RELEASE; UpdateTask task = new UpdateTask(); task.execute(url); } catch (NameNotFoundException e) { e.printStackTrace(); } } } class UpdateTask extends AsyncTask { boolean result = false; int newestVersion = 0; @Override protected Void doInBackground(String... arg0) { String requestUrl = arg0[0] + "?version=" + versionName + "&phone=" + phone_model + "&android=" + androidVersion; try { URL url = new URL(requestUrl); URLConnection conn = url.openConnection(); conn.setConnectTimeout(5); String resultStr = readIOStream( conn.getInputStream() ); resultStr = resultStr.trim(); newestVersion = Integer.parseInt(resultStr); result = true; } catch (Exception e) { Log.e("CheckUpdates", "Check for " + packageName + " failed!", e); } return null; } private String readIOStream(InputStream in) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte buf[] = new byte[1024]; int len; while ( (len=in.read(buf)) != -1) { baos.write(buf, 0, len); } return baos.toString(); } @Override protected void onPostExecute(Void r) { super.onPostExecute(r); Log.i("CheckUpdates", "result=" + result + ", newestVersion" + newestVersion + ", versionCode=" + versionCode); if (result == true) { if (newestVersion > versionCode) { AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setTitle(title); builder.setMessage("This application has an a newer version available. Update now?"); builder.setPositiveButton("Yes", new OnClickListener() { public void onClick(DialogInterface dialog, int which) { Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("market://search?q=pname:" + packageName) ); try { context.startActivity(i); } catch (Exception e) { Log.e("CheckUpdates", "Activity launch failed", e); } context = null; } }); builder.setNegativeButton("No", new OnClickListener() { public void onClick(DialogInterface dialog, int which) { context = null; } }); try { builder.show(); } catch (Exception e) { Log.e("CheckUpdates", "Builder.show failed", e); } } long now = System.currentTimeMillis(); SharedPreferences prefs = context.getSharedPreferences(PREFS, Context.MODE_PRIVATE); //when done write Editor edit = prefs.edit(); edit.putLong("lastCheck", now); edit.commit(); } } } }