服务绑定(BinderService)

为了使Service支持绑定,需要在Service类中重载onBind()方法,并在onBind()方法中返回Service对象

Service类

public class BinderService extends Service {

private static final String TAG = "BinderService";

private IBinder myBinder = new MyBinder();

@Override

public IBinder onBind(Intent intent) {

// TODO Auto-generated method stub

return myBinder;

}

public void MyMethod(){

Log.i(TAG, "MyMethod()");

}

public class MyBinder extends Binder{

public BinderService getService(){

return BinderService.this;

}

}

}

Activity类

package com.mzh.www;

import com.mzh.www.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.util.Log;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

/** 

* @Title: BinderServiceActivity.java

* @Package com.mzh.www

* @Description: 绑定服务

* @author mzh

* @version V2.2

*/

public class BinderServiceActivity extends Activity {

private static final String TAG = "BinderServiceActivity";

private Button startBinderBtn;

private Button stopBinderBtn;

private Boolean flag = false;

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.binder);

       

        startBinderBtn = (Button)findViewById(R.id.startBinderService);

        stopBinderBtn = (Button)findViewById(R.id.stopBinderService);

        startBinderBtn.setOnClickListener(listener);

        stopBinderBtn.setOnClickListener(listener);

    }

   

    private OnClickListener listener = new OnClickListener() {

@Override

public void onClick(View v) {

switch (v.getId()) {

case R.id.startBinderService:

bindService();

break;

case R.id.stopBinderService:

unbindService();

break;

default:

break;

}

}

};

private void unbindService() {

if(flag){

                          unbindService(conn);

                          Log.i(TAG, "unbindService()");

}

}

private void bindService() {

Intent intent = new Intent(BinderServiceActivity.this,BinderService.class);

bindService(intent, conn, Context.BIND_AUTO_CREATE);

}

private ServiceConnection conn = new ServiceConnection() {

@Override

public void onServiceDisconnected(ComponentName name) {

flag = false;

}

@Override

public void onServiceConnected(ComponentName name, IBinder service) {

MyBinder myBinder = (MyBinder)service;

BinderService binderService = myBinder.getService();

binderService.MyMethod();

flag = true;

}

};

}

取消绑定仅需要使用unbindService()方法,并将ServiceConnection()传递给unbindServicce()方法,但需要注意的是,unbindService()方法成功后,系统并不会调用onServiceConnected(),因为onServiceConnected()仅在意外断开绑定时才被调用 。

 小笔记:供自己加深记忆

  • 看这个时可以参考:file:///D:/Android_Environment/android-sdk_r12-windows/android-sdk-windows/docs/guide/developing/tools/aidl.html#CreateAidl

    AIDL(Andorid Interface Definition Language)接口描述语言

在Android系统中使用跨进程服务,一般按照以下三个步骤实现:

首先,使用AIDL语言定义跨进程服务的接口;

其次,根据AIDL语言定义的接口,在具体的Service类中实现接口中定义的方法和属性;

最后,在需要调用跨进程服务的组件中,通过相同的AIDL接口文件,调用跨进程服务。

在ANDROID中,不同进程不能访问其他进程的内存控件,为了使数据能够在不同进程意会北,数据必须转换成为能够穿越进程边界的系统级原语,同时,在数据完成穿越后,还需要转换回原来的格式。

记住此处一点:AIDL跟JAVA中的接口定义类似,只是AIDL可以定义参数传递的方向,而JAVA接口定义不行

AIDL支持三种方式 :in 、out、inout,

in:标识的参数将从调用者传递到跨进程服务中,其它同理推。

远程数据访问的创建和调用需要使用AIDL语言,一般有以下过程 :

1.使用AIDL语言定义跨进程服务和接口。

2.通过继承Service类实现跨进程服务。

3.绑定和使用跨进程服务。

数据传递:

进程间传递的数据类型除了JAVA支持的基本类型以外,还可以是自定义的数据类型,但用户自定义数据类型时要实现Parcelable接口,使自定义的数据类型能够转换为系统级原语保存在Parcel对象中,穿越进程边界后可再转换为初始格式

AIDL支持的数据类型如下:

1.JAVA语言的基本类型

2.String

3.CharSequence

4.List

5.Map

List Map指:其中所有的键和元素都必须是AIDL支持的数据类型

6.其它AIDL接口

7.Parcelable对象

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值