NotifyingServicepublic class NotifyingService extends android.app.Service implements RunnableThis is an example of service that will update its status bar balloon
every 5 seconds for a minute. |
Fields Summary |
---|
private static int | MOOD_NOTIFICATIONS | private final android.os.IBinder | mBinder | private android.app.NotificationManager | mNM |
Methods Summary |
---|
public android.os.IBinder | getBinder()
return mBinder;
| protected void | onCreate()
mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Start up the thread running the service. Note that we create a
// separate thread because the service normally runs in the process's
// main thread, which we don't want to block.
Thread thr = new Thread(null, this, "NotifyingService");
thr.start();
| protected void | onDestroy()
// Cancel the persistent notification.
mNM.cancel(MOOD_NOTIFICATIONS);
| public void | run()
try {
for (int i = 0; i < 4; ++i) {
Thread.sleep(5 * 1000);
setMoodRemoteView(R.drawable.stat_happy,
R.string.status_bar_notifications_happy_message);
Thread.sleep(5 * 1000);
setMoodRemoteView(R.drawable.stat_neutral,
R.string.status_bar_notifications_ok_message);
Thread.sleep(5 * 1000);
setMoodRemoteView(R.drawable.stat_sad,
R.string.status_bar_notifications_sad_message);
}
} catch (Exception ex) {
}
// Done with our work... stop the service!
this.stopSelf();
| private void | setMoodRemoteView(int moodId, int textId)
String str = getResources().getString(textId);
RemoteViews remoteViews = new RemoteViews(this.getPackageName(),
R.layout.status_bar_balloon);
remoteViews.setImageViewResource(R.id.icon, moodId);
remoteViews.setTextViewText(R.id.text, str);
Notification notification = new Notification(moodId, str, null, null, remoteViews);
mNM.notify(MOOD_NOTIFICATIONS, notification);
|
|