AlarmService_Servicepublic 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. |
Fields Summary |
---|
android.app.NotificationManager | mNM | Runnable | mTaskThe function that runs in our worker thread | private final android.os.IBinder | mBinderThis is the object that receives interactions from clients. See RemoteService
for a more complete example. |
Methods Summary |
---|
public android.os.IBinder | onBind(android.content.Intent intent)
return mBinder;
| public void | onCreate()
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 void | onDestroy()
// 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 void | showNotification()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);
|
|