开启Android四大组件中的Service

Service是Android的四大组件之一。Activity中开启Service有两种方式。一种调用startService(Intent intent)方法开启Service并通过stopService()方法结束 Service实例。另一种是通过bindService(Intentservice, ServiceConnection conn, int flags)的方式绑定Service调用Service的onBind(Intent intent)方法并通过unbindService(ServiceConnection conn)接触绑定。这两种之间的区别在于:第一种的方式时,Service的生命周期与Activity想关即当调用的Activity  finish()之后,Service 也随即的关闭。而第二种通过bindService的方式绑定的Service则不同,Service与Activity不行关。Activity 在finish之后,Service也依然运行。

再谈一下启动的过程startService方式第一次调用Service的时候会调用onCreate()的方法----onCreate()----onStartCommand(Intent intent, int flags, int startId)---  onStart方法但是当后续继续启动该Service时不会再继续调用该Service的oncreate()方法,当调用stopService的时候调用onDestroy.不管前面启动多少次Serivce,调用stopService会关闭所有的实例。

写一个Demo:

public class MainActivity extends Activity implements OnClickListener{

	private Button start,stop,bind,unbind;
	
	
	public ServiceConnection conn = new ServiceConnection() {
		
		@Override
		public void onServiceDisconnected(ComponentName name) {
			// TODO Auto-generated method stub
			
		}
		
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			// TODO Auto-generated method stub
			
		}
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		init();
	}

	private void init(){
		start = (Button)findViewById(R.id.start);
		stop = (Button)findViewById(R.id.stop);
		start.setOnClickListener(this);
		stop.setOnClickListener(this);
		
		bind = (Button)findViewById(R.id.bind);
		unbind = (Button)findViewById(R.id.unbind);
		bind.setOnClickListener(this);
		unbind.setOnClickListener(this);
	}
	
	public void onClick(View view){
		Intent intent = new Intent(MainActivity.this,TestService.class);
		if(view.getId()==R.id.start){
			startService(intent);
		}else if (view.getId()==R.id.stop) {
			stopService(intent);
		}
		else if (R.id.bind == view.getId()) {
			bindService(intent, conn, BIND_AUTO_CREATE);
		}else if (R.id.unbind == view.getId()) {
			unbindService(conn);
		}
	}

}
Service类

public class TestService extends Service {

	public BindHelper helper = new BindHelper();
	class BindHelper extends Binder{
		
	}
	
	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		Log.e("debug", "onBind");
		return null;
	}
	
	@Override
	@Deprecated
	public void onStart(Intent intent, int startId) {
		// TODO Auto-generated method stub
		Log.e("debug", "onStart");
		super.onStart(intent, startId);
	}
	
	
	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		Log.e("debug", "onCreate");
		super.onCreate();
	}
	
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		// TODO Auto-generated method stub
		Log.e("debug", "onStartCommand");
		return super.onStartCommand(intent, flags, startId);
	}
	
	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		Log.e("debug", "onDestroy");
		super.onDestroy();
	}

}

在配置文件中药注册。Service 详情: http://blog.csdn.net/wenzhenyu1990/article/details/16332497



你可能感兴趣的:(开启Android四大组件中的Service)