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

AlarmService_Service

public class AlarmService_Service extends android.app.Service
This is an example of implementing an application service that will run in response to an alarm, allowing us to move long duration work out of an intent receiver.
see
AlarmService
see
AlarmService_Alarm

Fields Summary
android.app.NotificationManager
mNM
Runnable
mTask
The function that runs in our worker thread
private final android.os.IBinder
mBinder
This is the object that receives interactions from clients. See RemoteService for a more complete example.
Constructors Summary
Methods Summary
public android.os.IBinderonBind(android.content.Intent intent)


    
        
        return mBinder;
    
public voidonCreate()

        mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

        // show the icon in the status bar
        showNotification();

        // 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, mTask, "AlarmService_Service");
        thr.start();
    
public voidonDestroy()

        // Cancel the notification -- we use the same ID that we had used to start it
        mNM.cancel(R.string.alarm_service_started);

        // Tell the user we stopped.
        Toast.makeText(this, R.string.alarm_service_finished, Toast.LENGTH_SHORT).show();
    
private voidshowNotification()
Show a notification while this service is running.

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

        // Set the icon, scrolling text and timestamp
        Notification notification = new Notification(R.drawable.stat_sample, text,
                System.currentTimeMillis());

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

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

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