Android四大组件Service之bindService启动

下面是绑定模式启动Service例程:

Android四大组件Service之bindService启动_第1张图片

BindService代码:

package com.example.ibcondservice02;

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

public class BindServiceText extends Service {  //Service 处理函数

	//1.创建一个binder实例
	private MyBinder binder = new  MyBinder(); // 创建一个binder类,类中能返回Service 对象(MyBinder)并创建一个实例 binder
	
	public class MyBinder extends Binder{  // 创建一个Binder实例
		
		public BindServiceText getService()   // 返回当前服务对象
		{
			return BindServiceText.this;
		}
		
	}
	
	// 2.返回一个binder实例 , 传入到onServiceConnected 的IBinder参数中
	@Override
	public IBinder onBind(Intent arg0) {  //传入到onServiceConnected 的IBinder参数中
		// TODO Auto-generated method stub
		return binder;   // 返回binder对象
	}
	
	
	public void MyMethod()  // 创建一个公共的方法
	{
		System.out.println("BinderService!!!");
	}
	

}

Activity代码:

package com.example.ibcondservice02;

import com.example.ibcondservice02.BindServiceText.MyBinder;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
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 IboundService extends Activity {

	
	private Button StartButton;
	private Button StopButton;
	private Boolean isConnected=false;	 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_ibound_service);
		
		StartButton = (Button)findViewById(R.id.iboundStart);
		StopButton = (Button)findViewById(R.id.iboundStop);
		
		StartButton.setOnClickListener(listener);
		StopButton.setOnClickListener(listener);
				
	}
	
	OnClickListener listener = new OnClickListener() {	
		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			
		
			
			switch (arg0.getId()) {
			case R.id.iboundStart:
				bindService();
			
				break;
			case R.id.iboundStop:				
				unbindService();
				break;
			default:
				break;
			}
			
		}
	};
	
	private void bindService() {
		// TODO Auto-generated method stub
		Intent intent2 = new Intent(IboundService.this,BindServiceText.class);
		bindService(intent2, conn, Context.BIND_AUTO_CREATE);
		
	}
	
	private void unbindService() {
		// TODO Auto-generated method stub
		if(isConnected)
		{
			unbindService(conn);
		}
		
	}
	
	private ServiceConnection conn= new ServiceConnection()  {
		
		// 建立绑定时启动,IBinder对象(binder)参数是在IBinder onBind();得到的对象,《------
		@Override
		public void onServiceConnected(ComponentName arg0, IBinder binder) {
			// TODO Auto-generated method stub
			MyBinder mybinder=(MyBinder)binder;  // 获取的是binderService类中onBind返回的binder对象
												// 
			BindServiceText Service = mybinder.getService();
			
			Service.MyMethod();
			
			isConnected=true;
		}
		
		@Override
		public void onServiceDisconnected(ComponentName arg0) {
			// TODO Auto-generated method stub
			isConnected=false;
		}
	};
}


Androidmanifest.xml

 




 

你可能感兴趣的:(Android,android,-,四大组件)