FileDocCategorySizeDatePackage
NotifyingService.javaAPI DocAndroid 1.5 API5005Wed May 06 22:41:08 BST 2009com.example.android.apis.app

NotifyingService

public class NotifyingService extends android.app.Service
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 android.os.ConditionVariable
mCondition
private Runnable
mTask
private final android.os.IBinder
mBinder
private android.app.NotificationManager
mNM
Constructors Summary
Methods Summary
public android.os.IBinderonBind(android.content.Intent intent)


    
        
        return mBinder;
    
public 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 notifyingThread = new Thread(null, mTask, "NotifyingService");
        mCondition = new ConditionVariable(false);
        notifyingThread.start();
    
public voidonDestroy()

        // Cancel the persistent notification.
        mNM.cancel(MOOD_NOTIFICATIONS);
        // Stop the thread from generating further notifications
        mCondition.open();
    
private voidshowNotification(int moodId, int textId)

        // In this sample, we'll use the same text for the ticker and the expanded notification
        CharSequence text = getText(textId);

        // Set the icon, scrolling text and timestamp.
        // Note that in this example, we pass null for tickerText.  We update the icon enough that
        // it is distracting to show the ticker text every time it changes.  We strongly suggest
        // that you do this as well.  (Think of of the "New hardware found" or "Network connection
        // changed" messages that always pop up)
        Notification notification = new Notification(moodId, null, System.currentTimeMillis());

        // The PendingIntent to launch our activity if the user selects this notification
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                new Intent(this, NotifyingController.class), 0);

        // Set the info for the views that show in the notification panel.
        notification.setLatestEventInfo(this, getText(R.string.status_bar_notifications_mood_title),
                       text, contentIntent);

        // Send the notification.
        // We use a layout id because it is a unique number.  We use it later to cancel.
        mNM.notify(MOOD_NOTIFICATIONS, notification);