ServiceStartArgumentspublic class ServiceStartArguments extends android.app.Service This is an example of implementing an application service that runs locally
in the same process as the application. The {@link ServiceStartArgumentsController}
class shows how to interact with the service.
Notice the use of the {@link NotificationManager} when interesting things
happen in the service. This is generally how background services should
interact with the user, rather than doing something more disruptive such as
calling startActivity(). |
Fields Summary |
---|
private android.app.NotificationManager | mNM | private android.content.Intent | mInvokeIntent | private volatile android.os.Looper | mServiceLooper | private volatile ServiceHandler | mServiceHandler |
Methods Summary |
---|
public android.os.IBinder | onBind(android.content.Intent intent)
return null;
| public void | onCreate()
mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
// This is who should be launched if the user selects our persistent
// notification.
mInvokeIntent = new Intent(this, ServiceStartArgumentsController.class);
// 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.
HandlerThread thread = new HandlerThread("ServiceStartArguments");
thread.start();
mServiceLooper = thread.getLooper();
mServiceHandler = new ServiceHandler(mServiceLooper);
| public void | onDestroy()
mServiceLooper.quit();
// Cancel the persistent notification.
mNM.cancel(R.string.service_arguments_started);
// Tell the user we stopped.
Toast.makeText(ServiceStartArguments.this, R.string.service_arguments_stopped,
Toast.LENGTH_SHORT).show();
| public void | onStart(android.content.Intent intent, int startId)
Log.i("ServiceStartArguments",
"Starting #" + startId + ": " + intent.getExtras());
Message msg = mServiceHandler.obtainMessage();
msg.arg1 = startId;
msg.obj = intent.getExtras();
mServiceHandler.sendMessage(msg);
Log.i("ServiceStartArguments", "Sending: " + msg);
| 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.service_arguments_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.service_start_arguments_label),
text, contentIntent);
// Send the notification.
// We use a string id because it is a unique number. We use it later to cancel.
mNM.notify(R.string.service_arguments_started, notification);
|
|