Android Service Example

August 10, 2011

The steps to create a self-contained service associated with a single component are
1. Create a class to extend Service. (In Eclipse, this can be done by right-clicking
the project, choosing New → Class and specifying as the
super class.)

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class SimpleActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
Button startButton = (Button) findViewById(;
startButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view){
startService(new Intent(SimpleActivity.this,
Button stopButton = (Button)findViewById(;
stopButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
stopService(new Intent(SimpleActivity.this,


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: