FileDocCategorySizeDatePackage
NotifyingService.javaAPI DocGoogle Android v1.5 Example3654Sun Nov 11 13:01:04 GMT 2007com.google.android.samples.app

NotifyingService

public class NotifyingService extends android.app.Service implements Runnable
This 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
Constructors Summary
Methods Summary
public android.os.IBindergetBinder()

        return mBinder;
    
protected voidonCreate()

    
    
       
        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 voidonDestroy()

        // Cancel the persistent notification.
        mNM.cancel(MOOD_NOTIFICATIONS);
    
public voidrun()

        
        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 voidsetMoodRemoteView(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);