TestServiceActivity.java
package com.wanghao.service;
import android.app.Activity;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class TestServiceActivity extends Activity {
private Button startServiceButton = null;
private Button stopServiceButton = null;
private Button bindServiceButton = null;
private Button unBindServiceButton = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startServiceButton = (Button) findViewById(R.id.startService);
startServiceButton.setOnClickListener(new StartServiceListener());
stopServiceButton = (Button) findViewById(R.id.stopService);
stopServiceButton.setOnClickListener(new StopServiceListener());
bindServiceButton = (Button)findViewById(R.id.bindService);
bindServiceButton.setOnClickListener(new BindServiceListener());
unBindServiceButton = (Button)findViewById(R.id.unBindService);
unBindServiceButton.setOnClickListener(new UnBindServiceListener());
System.out.println("Activity onCreate");
}
// 启动Service监听器
class StartServiceListener implements OnClickListener {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(TestServiceActivity.this, MyService.class);
startService(intent);
}
}
// 停止Service监听器
class StopServiceListener implements OnClickListener {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(TestServiceActivity.this, MyService.class);
stopService(intent);
}
}
// 连接对象
private ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
System.out.println("Bind success!");
}
@Override
public void onServiceDisconnected(ComponentName name) {
System.out.println("UnBind success!");
}
};
// 绑定Service监听器
class BindServiceListener implements OnClickListener {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(TestServiceActivity.this, MyService.class);
bindService(intent, conn, Service.BIND_AUTO_CREATE);
}
}
// 解除绑定Service监听器
class UnBindServiceListener implements OnClickListener {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(TestServiceActivity.this, MyService.class);
unbindService(conn);
}
}
}
服务类MyService.java
package com.wanghao.service;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class MyService extends Service {
// 必须实现的一个方法,返回一个绑定的接口给Service
@Override
public IBinder onBind(Intent intent) {
System.out.println("Service onBind");
return null;
}
@Override
public boolean onUnbind(Intent intent) {
System.out.println("Service onUnBind");
return super.onUnbind(intent);
}
// 当Service第一次被创建时,由系统调用
@Override
public void onCreate() {
System.out.println("Service onCreate");
super.onCreate();
}
// 当Service不再使用,系统调用该方法
@Override
public void onDestroy() {
System.out.println("Service onDestroy");
super.onDestroy();
}
// 当通过startService()方法启动Service时,该方法被调用,API level 5之后建议复写onStartCommand()方法
@Override
public void onStart(Intent intent, int startId) {
System.out.println("Service onStart");
super.onStart(intent, startId);
}
// Called by the system every time a client explicitly starts the service by calling startService(Intent), providing the arguments it supplied and a unique integer token representing the start request. Do not call this method directly.
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
System.out.println("Service onStartCommand");
return super.onStartCommand(intent, flags, startId);
}
}