Saturday, June 19, 2010

Toast

Toast is quite a useful and compact widget class to provide a temporal notification for the user.

Toast.makeText(KeasDroid.this,
"Here you can maintain your user credentials.",
Toast.LENGTH_LONG).show();



This is the equivalent of a modal dialog box, a timer and a dismiss of the dialog. There doesn't appear to be a reasonable way of prolonging the length of display beyond a few secs.

To notify on the status bar requires more work, here's a simple example:

String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager)
getSystemService(ns);


int icon = R.drawable.icon;
CharSequence tickerText = "Click Menu to set username";
long when = System.currentTimeMillis();

Notification notification = new Notification(icon, tickerText, when);
Context context = getApplicationContext();
CharSequence contentTitle = "Keas notification";
CharSequence contentText = "Please click Menu to set username";
Intent notificationIntent = new Intent(this, KeasDroid.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);


notification.setLatestEventInfo(context, contentTitle, contentText,
contentIntent);

final int HELLO_ID = 1;

mNotificationManager.notify(HELLO_ID, notification);

No comments:

Post a Comment