Tutorial details

Android beginner tutorial: Part 61 - Creating, starting and stopping a Service | App Code for Sale | Preview

Android beginner tutorial: Part 61 - Creating, starting and stopping a Service | Android Tutorial

How to create, start and stop a Service

Overview PAGE TOP

In this tutorial we will learn how to create, start and stop a Service.

Creating a Service is similar to creating an Activity. We have to create a class of it and declare it in the Manifest. A Service has no xml layout though, since it doesn't have a user interface.

Open the Manifest using Eclipse and add the service. There two ways to do this.

One way is to directly add this to the application node in the xml:

<service android:name="TestService"></service>

The second way is to go to the Application tab and add a new Service object using the "Add..." button in the Application Nodes box. Click the blue "Name*" label next to the Application Nodes box when the new Service is selected and it will let you create a class for your service right away. This will both create the class for the service and add it to the manifest xml.

In the TestService.java class we'll declare 4 functions - onBind(), onCreate(), onStartCommand() and onDestroy(). Let's Toast some messages in the last 3 functions so that we know when those events are caught.

The onStartCommand() method must return a value based on when we want our service to stop working.We need to return START_STICKY if we want to keep that service running until it is explicitly stopped. That's what we're going to return this time.

package com.kircode.codeforfood_test;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;

public class TestService extends Service {

@Override
public IBinder onBind(Intent intent){
return null;
}

@Override
public void onCreate(){
Toast.makeText(this, "TestService created", Toast.LENGTH_SHORT).show();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "TestService started", Toast.LENGTH_SHORT).show();
return START_STICKY;
}

@Override
public void onDestroy(){
Toast.makeText(this, "TestService destroyed", Toast.LENGTH_SHORT).show();
}
}

Now let's go to the layout xml of the launcher application, activity_main.xml, and add 2 buttons - one that starts the service and one that stops it.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <Button android:id="@+id/startButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Start service"
        />

    <Button android:id="@+id/stopButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Stop service"
        />

</LinearLayout>

In the class file of the Activity add listeners to these buttons and call startService() and stopService() methods, which contain Intents in the parameters that direct to the Service.

package com.kircode.codeforfood_test;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

final Button startbtn = (Button)findViewById(R.id.startButton);
startbtn.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, TestService.class);
startService(intent);
}
});

final Button stopbtn = (Button)findViewById(R.id.stopButton);
stopbtn.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, TestService.class);
stopService(intent);
}
});
}

}

Now you can test your application. When you start a new Service, you'll see that both onCreate() and onStartCommand() are executed. If you keep calling startService() when its already created, it will only execute onStartCommand(). No matter how many times startService() was called, it can only be stopped once by stopService(). After that, it can be created and started again.

That's all for today.

Thanks for reading!

Reference PAGE TOP

http://kirill-poletaev.blogspot.it/2013/01/android-beginner-tutorial-part-61.html

0 Comments Leave a comment

Please login in order to leave a comment.

Newest first
!

Sign-in to your Chupamobile Account.

The Easiest way to Launch your next App or Game.

Join Chupamobile and get instant access to thousands of ready made App and Game Templates.

Creating an account means you’re okay with Chupamobile’s Terms of Service and Privacy Policy.