Android之Service学习篇二:Service启动方式之boundService

上一篇中介绍了Service的第一种方式,startService,这一篇来讲解一下另一张方式 bindService。

当创建一个能提供绑定功能的服务时,我们必须提供一个IBinder对象,客户端能使用这个对象与服务进行交换。在Android中有三种定义方式:

1、扩展Binder类 (条件:服务和应用在同一个进程当中,是最常见的情况)

2、使用Messager

3、使用AIDL (Android Interface Defination Language)


Bound Services

A bound service is the server in a client-server interface. A bound service allows components (such as activities) to bind to the service, send requests, receive responses, and even perform interprocess communication (IPC). A bound service typically lives only while it serves another application component and does not run in the background indefinitely.


这里以扩展Binder类为例讲解如何创建Bound Services.

Binder

extends  Object
implements  IBinder
java.lang.Object
   ↳ android.os.Binder
很明显,Binder实现了IBinder这接口。

通过扩展Binder类创建Bound Service的步骤如下:

a、在Service类中,创建一个Binder实例,包含客户端能调用的公共方法,返回当前服务对象;

b、在onBind()方法中返回Binder实例;

c、在客户端,从onServiceConnected方法中获得Binder实例。


工程目录结构:


运行界面:


源代码:

MainActivity.java:

package com.service.activity;

import com.service.activity.BinderService.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 MainActivity extends Activity {
	private Button btnStartService;
	private Button btnstopService;
	private boolean isConnected = false;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btnStartService = (Button)findViewById(R.id.btnStartService);
        btnStartService.setOnClickListener(listener);
        btnstopService = (Button)findViewById(R.id.btnStopService);
        btnstopService.setOnClickListener(listener);
    }
    
    private OnClickListener listener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.btnStartService:
				funBindService();
				break;
			case R.id.btnStopService:
				funUnBindService();
				break;
			default:
				break;
			}
		}
		private void funBindService() {
			Intent intent = new Intent(MainActivity.this, BinderService.class);
			bindService(intent, conn, Context.BIND_AUTO_CREATE);
		}

		private void funUnBindService() {
			if(isConnected == true){
				unbindService(conn);
			}
		}
		
	};
	
	private ServiceConnection conn = new ServiceConnection() {
		
		@Override
		public void onServiceDisconnected(ComponentName name) {
			isConnected = false;
		}
		//与Service连接时被回调
		@Override
		public void onServiceConnected(ComponentName name, IBinder iBinder) {
			MyBinder myBinder = (MyBinder)iBinder;
			BinderService service = myBinder.getService();
			service.MyMethod();
			isConnected = true;
		}
	};
}

BinderService.java

package com.service.activity;

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

public class BinderService extends Service{
	private static final String TAG = "BinderService";
	private MyBinder myBinder = new MyBinder();
	//内部类,扩展自Binder类
	public class MyBinder extends Binder{
		public BinderService getService(){
			return BinderService.this;
		}
	}
	//复写onBind方法,并且返回IBinder的实现类
	@Override
	public IBinder onBind(Intent intent) {
		Log.i(TAG, "onBind");
		return myBinder;
	}
	
	public void MyMethod(){
		Log.i(TAG, "MyMethod");
	}

	@Override
	public boolean onUnbind(Intent intent) {
		Log.i(TAG, "onUnbind");
		return super.onUnbind(intent);
	}
	
	@Override
	public void onDestroy() {
		Log.i(TAG, "onDestroy");
		super.onDestroy();
	}
	
}



main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button 
	    android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="启动service"
        android:id="@+id/btnStartService"/>
	<Button 
	    android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="停止service"
        android:id="@+id/btnStopService"/>

</LinearLayout>

点击启动service,日志输出信息:


点击停止service,日志输出信息:



  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值