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 android.app.Service as the
super class.)

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class SimpleActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button startButton = (Button) findViewById(R.id.Button01);
startButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view){
startService(new Intent(SimpleActivity.this,
SimpleService.class));
}
});
Button stopButton = (Button)findViewById(R.id.Button02);
stopButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
stopService(new Intent(SimpleActivity.this,
SimpleService.class));
}
});
}
}

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: