Posts Tagged Started Service in Android

Started Service in Android

Here in this post I would like to discuss about Started Service.

First of all what we need to do is create a Service.

How is it done?

There are basically two ways for that. First one is to extend the class Service which is the base class for all Services. Second option is extending IntentService which is the subclass of the Service class.

It’s wise that we create a subclass of the class Service when there are a multiple requests for our service to start simultaneously.

IntentService is used when our service needs to handle all start requests, one at a time and not simultaneously.  So here, I’m extending  IntentService  in my program as I’m just showing a Toast and would display a message at the LogCat.

So here we go 🙂

Let’s see the XML code for the layout.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
    <Button
        android:id="@+id/startbtn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/textbtn" />
</LinearLayout>

Here, I’ve placed a button in MyStartedServiceActivity which is my activity, clicking on which would start the service.

Let’s see the code:

package com.deepthi.mystartedservice;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MyStartedServiceActivity extends Activity {
    /** Called when the activity is first created. */
      Button start;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        start=(Button)findViewById(R.id.startbtn);
        start.setOnClickListener(new OnClickListener() {
                  public void onClick(View v) {
                        // TODO Auto-generated method stub
                        Intent intent=new Intent(getApplicationContext(), myservice.class);
                        startService(intent);
                  }
            });
    }
}

Just look at the above code. Here the service is started by passing an Intent which specifies the Service to be started, to startService().  The system then calls the onStartCommand() and passes the intent to it.

Now, let’s look at the code of myservice :

package com.deepthi.mystartedservice;
import android.app.IntentService;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
public class myservice extends IntentService{
      public myservice() {
            super("myservice");
            // TODO Auto-generated constructor stub
      }
      @Override
      protected void onHandleIntent(Intent intent) {
            // TODO Auto-generated method stub
            Log.w("TAG","Started my Service");
      }
      @Override
      public int onStartCommand(Intent intent, int flags, int startId) {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), "Starting my Service", Toast.LENGTH_LONG).show();
            return super.onStartCommand(intent, flags, startId);
      }
}

Here, we just need a constructor and then we need to override and implement onHandleIntent() .

Note: Don’t forget to register service at the Manifest File.

Here is the Manifest File:

<application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".MyStartedServiceActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service
            android:name=".myservice"/>
    </application>

The Screen Shots of the Out Put are shown below:

Starting Service by clicking the button.

The LogCat :

Hope this post was useful. 🙂

, , , , , , ,

3 Comments