Android startService 和 bindService

Android startService 和 bindService
通过Context的startService()方法:通过该方法启动Service,访问者与Service之间没有关联,即使访问者退出了,Service也仍然运行;
通过Context的bindService()方法:使用该方法启动Service,访问者与Service绑定在一起,访问者一旦退出,Service也就种植了。

MyAppService类代码:
package com.example.binder;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

public class MyAppService extends Service {

	private String str;
	public class MyBinder extends Binder {

		public MyAppService getService() {
			return MyAppService.this;
		}
	}

	@Override
	public void onCreate() {
		Log.d(this.getClass().getName(), "onCreate");
	}

	// 需要Service处理的操作在此
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		str=str+",world!";
		Log.d("onStartCommand",str);
		
		return super.onStartCommand(intent, flags, startId);
	}

	public String getServiceValue() {
		return str;
	}

	public void setServiceValue(String str) {
		this.str = str;
	}

	@Override
	public IBinder onBind(Intent intent) {
		return new MyBinder();
	}

	@Override
	public boolean onUnbind(Intent intent) {
		return super.onUnbind(intent);
	}

	@Override
	public void onDestroy() {
		
	}
}

MainActivity代码:
package com.example.binder;

import com.example.binder.MyAppService.MyBinder;

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.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

	private	ServiceConnection sc;
	private MyAppService myAppService;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		sc = new ServiceConnection() {

			@Override
			public void onServiceDisconnected(ComponentName name) {

			}

			@Override
			public void onServiceConnected(ComponentName name, IBinder service) {
				MyBinder mBinder = (MyBinder) service;
				myAppService = mBinder.getService();
			}
		};
		
		Button start=(Button) findViewById(R.id.start);
		start.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				startService();
			}
		});
		
		Button bind=(Button) findViewById(R.id.bind);
		bind.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				bindService();
			}
		});
		
		Button getValue=(Button) findViewById(R.id.get);
		getValue.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String str = myAppService.getServiceValue();
				Log.d("服务获取的值", str);
			}
		});
	}
	
	
	private	void	startService(){
		myAppService.setServiceValue("hello");
		
		Intent intent=new Intent(this,MyAppService.class);
		startService(intent);
	}

	private	void	bindService(){
		Intent mIntent = new Intent(this, MyAppService.class);
		bindService(mIntent, sc, Service.BIND_AUTO_CREATE);
		//BIND_AUTO_CREATE自动创建
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();

		Intent intent = new Intent(this, MyAppService.class);
		stopService(intent);
	}
}

AndroidManiFest.xml里面注册MyappService

<service android:name=".MyAppService"/>

你可能感兴趣的:(android)