--- android/CheckUpdates/src/dk/thoerup/checkupdates/CheckUpdates.java 2010/06/30 09:14:22 942 +++ android/CheckUpdates/src/dk/thoerup/checkupdates/CheckUpdates.java 2010/06/30 21:55:56 943 @@ -7,6 +7,9 @@ import java.net.URLConnection; import android.app.AlertDialog; +import android.app.Notification; +import android.app.NotificationManager; +import android.app.PendingIntent; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; @@ -22,6 +25,7 @@ public class CheckUpdates { final static long TIMESPAN_DAY = 24*60*60*1000; // one day + final static String CHECKUPDATES = "CheckUpdates"; final static String LASTCHECK = "lastcheck"; @@ -138,46 +142,69 @@ edit.putLong(LASTCHECK, now); edit.commit(); - 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) { - String launchUrl = (apkUrl == null) ? "market://search?q=pname:" + packageName : apkUrl; - - Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(launchUrl) ); - i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK ); - 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; - } - }); + if (newestVersion > versionCode) { + String launchUrl = (apkUrl == null) ? "market://search?q=pname:" + packageName : apkUrl; + //showUpdateDialog(launchUrl); + showNotification(launchUrl); + } + + } + + } + + private void showNotification(String launchUrl) { + NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); + int icon = android.R.drawable.sym_action_email; //TODO: find a better icon + + Notification notification = new Notification(icon, "Update available", System.currentTimeMillis() ); + notification.flags |= Notification.FLAG_AUTO_CANCEL ; + + CharSequence contentTitle = title; + CharSequence contentText = "New version available."; + Intent notificationIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(launchUrl) ); + PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0); + + notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); + + nm.notify(1, notification); + + } + + private void showUpdateDialog(final String launchUrl) { + 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(launchUrl) ); + i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK ); try { - builder.show(); + context.startActivity(i); } catch (Exception e) { - Log.e(CHECKUPDATES, "Builder.show failed", 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); + } } } } +