FileDocCategorySizeDatePackage
RemoteService.javaAPI DocAndroid 1.5 Example6690Tue Aug 09 22:58:18 BST 2011com.example.android.apis.app

RemoteService

public class RemoteService extends android.app.Service
This is an example of implementing an application service that runs in a different process than the application. Because it can be in another process, we must use IPC to interact with it. The {@link RemoteServiceController} and {@link RemoteServiceBinding} classes show how to interact with the service.

Fields Summary
final android.os.RemoteCallbackList
mCallbacks
This is a list of callbacks that have been registered with the service. Note that this is package scoped (instead of private) so that it can be accessed more efficiently from inner classes.
int
mValue
android.app.NotificationManager
mNM
private final IRemoteService.Stub
mBinder
The IRemoteInterface is defined through IDL
private final ISecondary.Stub
mSecondaryBinder
A secondary interface to the service.
private static final int
REPORT_MSG
private final android.os.Handler
mHandler
Our Handler used to execute operations on the main thread. This is used to schedule increments of our value.
Constructors Summary
Methods Summary
public android.os.IBinderonBind(android.content.Intent intent)

        // Select the interface to return.  If your service only implements
        // a single interface, you can just return it here without checking
        // the Intent.
        if (IRemoteService.class.getName().equals(intent.getAction())) {
            return mBinder;
        }
        if (ISecondary.class.getName().equals(intent.getAction())) {
            return mSecondaryBinder;
        }
        return null;
    
public voidonCreate()

    
    
       
        mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

        // Display a notification about us starting.
        showNotification();
        
        // While this service is running, it will continually increment a
        // number.  Send the first message that is used to perform the
        // increment.
        mHandler.sendEmptyMessage(REPORT_MSG);
    
public voidonDestroy()

        // Cancel the persistent notification.
        mNM.cancel(R.string.remote_service_started);

        // Tell the user we stopped.
        Toast.makeText(this, R.string.remote_service_stopped, Toast.LENGTH_SHORT).show();
        
        // Unregister all callbacks.
        mCallbacks.kill();
        
        // Remove the next pending message to increment the counter, stopping
        // the increment loop.
        mHandler.removeMessages(REPORT_MSG);
    
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.remote_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, LocalServiceController.class), 0);

        // Set the info for the views that show in the notification panel.
        notification.setLatestEventInfo(this, getText(R.string.remote_service_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.remote_service_started, notification);