创建Service需要继承android.app.Service类。Service的生命周期回掉方法主要有六个。onCreat,onDestroy,onStart,onBind,onUnBind,onRebind。
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.Handler;
import android.os.IBinder;
import android.os.Vibrator;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
ServiceConnection sc ;
OnClickListener listener ;
Handler hd = new Handler(){public void handleMessage(android.os.Message msg) {
switch(msg.what){
case 0 : Toast.makeText(MainActivity.this, "onBind", Toast.LENGTH_LONG).show();break ;
case 1 : Toast.makeText(MainActivity.this, "onUnBind", Toast.LENGTH_LONG).show();break ;
case 2 : Toast.makeText(MainActivity.this, "onDestroy", Toast.LENGTH_LONG).show();break ;
case 3 : Toast.makeText(MainActivity.this, "onCreate", Toast.LENGTH_LONG).show();break ;
}
};} ;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.w6_1) ;
sc = new ServiceConnection() {
public void onServiceDisconnected(ComponentName name) {
}
public void onServiceConnected(ComponentName name, IBinder service) {
}
};
Button btn1 = (Button) findViewById(R.id.w6_1_btn1) ;
Button btn2 = (Button) findViewById(R.id.w6_1_btn2) ;
Button btn3 = (Button) findViewById(R.id.w6_1_btn3) ;
Button btn4 = (Button) findViewById(R.id.w6_1_btn4) ;
listener = new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,MyService.class) ;
switch(v.getId()){
case R.id.w6_1_btn1 : startService(intent);hd.sendEmptyMessage(3); break ;
case R.id.w6_1_btn2 : stopService(intent);hd.sendEmptyMessage(2);break ;
case R.id.w6_1_btn3 : bindService(intent,sc,BIND_AUTO_CREATE);hd.sendEmptyMessage(0);break ;
case R.id.w6_1_btn4 : unbindService(sc) ;hd.sendEmptyMessage(1) ;break ;
}
}
};
btn1.setOnClickListener(listener);
btn2.setOnClickListener(listener);
btn3.setOnClickListener(listener);
btn4.setOnClickListener(listener);
}
}
package com.example.a_wuyafeng;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class MyService extends Service {
public IBinder onBind(Intent intent) {
System.out.println("onBind");
return null;
}
@Override
public void onCreate() {
// TODO 自动生成的方法存根
super.onCreate();
System.out.println("oncreate");
}
@Override
public void onDestroy() {
// TODO 自动生成的方法存根
super.onDestroy();
System.out.println("ondestroy");
}
@Override
public boolean onUnbind(Intent intent) {
// TODO 自动生成的方法存根
System.out.println("onunbind");
return super.onUnbind(intent);
}
@Override
public void onRebind(Intent intent) {
// TODO 自动生成的方法存根
super.onRebind(intent);
System.out.println("onRebind");
}
@Override
public void onStart(Intent intent, int startId) {
// TODO 自动生成的方法存根
super.onStart(intent, startId);
System.out.println("start");
}
}