Local Service | Android Tutorial for Beginners – (Part 7)

Service is a fundamental component in android. Many a time, applications will need to run processes for a long time without any intervention from the user, or very rare interventions. These background processes need to keep running even when the phone is being used for other activities / tasks. 

To accommodate for such a requirement, android has introduced the “Service” component. It is a long lived component and does not implement any user interface of its own. 

Typically a user interacts with a service through an activity that has a UI. The service by itself “notifies” the user in case of any need for user intervention. 

In this article, I would like to introduce you to a very simple service which runs in the background but does not have any notification features. 

In order to start and stop the service, I have created an activity called the service controller.

In my example, when the service starts, it toasts a message that the service has started. When the service ends, it toasts a message that the service has ended. This is not of much use in the real sense of services. However, it simplifies the introduction of service creation and destruction. 

Ideally, one of the ways could be: a service that is running should notify itself as running by putting up a status bar notification as discussed in the previous article. And when the service has completed running the notification can be removed. Through the notification, the service can give a user interface for binding to the service or viewing the status of the service or any other similar interaction with the service. The combination of service with notification will be an example I will give in the next part.

Before we jump into the example, a little more about services. 

Binding to a service:
Once a service has begun and is running in the background, any number of activities can “bind” to the service. In fact, if we want to bind to a service that has not started, calling to bind could initiate the service. Such a service would shut down as soon as the last user detaches from the service. 

Remote service
The services defined above are those that run in the same process as the application that started it. However, we can have services that run in its own process. This is particularly useful when the service has to run for a long time, typical example being a background music player. For two processes to communicate, the object being passed needs to be marshaled.

For this, Android provides a AIDL tool (Android Interface Definition Language) to handle all marshalling and communication. The remote service example will be taken up in a subsequent article.

Service Example

Step 1:
Let us start with creating a service class that extends android.app.Service.
This service just displays a message when started and again displays a message when stopped. Hence theonStart() and onDestroy() methods are implemented. Here is the code.

public class SimpleService extends Service {
      @Override
      public IBinder onBind(Intent arg0) {
            return null;
      }
      @Override
      public void onCreate() {
            super.onCreate();
            Toast.makeText(this,"Service created ...", Toast.LENGTH_LONG).show();
      }
      
      @Override
      public void onDestroy() {
            super.onDestroy();
            Toast.makeText(this"Service destroyed ...", Toast.LENGTH_LONG).show();
      }
}

Step 2: 
An entry for this service needs to be made in the AndroidManifest.xml file. Here it is:

            <service android:name=".SimpleService">
            </service>

Step 3: 
Now, we need to be able to invoke this service. i.e. start the service and stop the service. I choose to write an activity that can start and stop the service. This activity is called SimpleServiceController.
Here is what it does:
To start the service – 

              Button start = (Button)findViewById(R.id.serviceButton);
              start.setOnClickListener(startListener);
       private OnClickListener startListener = new OnClickListener() {
            public void onClick(View v){
startService(newIntent(SimpleServiceController.this,SimpleService.class));
            }                 
       };

Similarly to stop the service – 
              Button stop = (Button)findViewById(R.id.cancelButton);
              stop.setOnClickListener(stopListener);
       private OnClickListener stopListener = new OnClickListener() {
            public void onClick(View v){
stopService(newIntent(SimpleServiceController.this,SimpleService.class));
            }                 
          };

Step 4:
The entry for this class in the AndroidManifest.xml file is as usual:
        <activity android:name=".SimpleServiceController"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

Now we can launch this application and we can start and stop a service.

Search & Download lots of Eclipse-Android Tutorials:-
  Using Pdf Wallet you can find useful and free Android Tutorials.


Search 

Please Select File-Format

PDF  - eBooks in pdf format.
PPT  - Presentations:2003.
PPTX- Presentations:2007+.
DOC - Microsoft Word Files:2003.
DOCXMicrosoft Word Files:2007+.
XLS  - Excel File format:2003.
XLSX - Excel File Format:2007+.
CHM - Compiled Help Files.
RTF  - Rich Text format.
TXT  - Text Files. 

No comments:

Post a Comment