Android 使用binder访问service的方式

binder机制是贯穿整个Android系统的进程间访问机制,经常被用来访问service,我们结合代码看一下binder在访问service的情形下是怎么具体使用的。

service 你可以理解成没有的界面的activity,它是跑在后台的程序,所谓后台是相对于可以被看得到的程序的,后台程序是不能直接交互的程序。

binder主要是用来进程间通信的,但也可用在和本地service通信。

1. 我们先来看一个与本地service通信的例子。

package com.ckt.wangxin;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.widget.Toast;
/**
 * This is a service stub for both LocalBinderClient
 * and RemoteBinderClient
 * @author Wang Xin
 * @email [email protected]
 *
 */
public class LocalService extends Service {

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

    public void sayHelloWorld(){
        Toast.makeText(this.getApplicationContext(), "Hello World Local Service!", Toast.LENGTH_SHORT).show();
    }   

    public class LocalBinder extends Binder {
        LocalService getService() {
            // Return this instance of LocalService so clients can call public methods
            return LocalService.this;
        }
    }
}

local servcie 的代码如上,在onBinder方法中返回binder,binder包含了service的句柄,客户端得到句柄以后就可以调用servcie的公共方法了,这种调用方式是最常见的。

客户端代码

package com.ckt.wangxin;

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 com.ckt.wangxin.LocalService.LocalBinder;

public class LocalServiceTestActivity extends Activity {
    static final String TAG = "LocalBinderTestActivity";
    ServiceConnection mSc;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mSc = new ServiceConnection(){
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                Log.d(TAG, "service connected");
                LocalService ss = ((LocalBinder)service).getService();
                ss.sayHelloWorld();
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {
                Log.d(TAG, "service disconnected");
            }
        };
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d(TAG, this.getApplicationContext().getPackageCodePath());
        Intent service = new Intent(this.getApplicationContext(),LocalService.class);
        this.bindService(service, mSc, Context.BIND_AUTO_CREATE);
    }

    @Override
    protected void onStop() {
        super.onStop();
        //must unbind the service otherwise the ServiceConnection will be leaked.
        <span style="color: rgb(255, 0, 0); ">this.unbindService(mSc);</span>
    }
}

需要注意的是在onStop中要解绑定service, 否则会造成内存泄露的问题。

2. 我们再看一下与另外一个进程中的service进行通信的问题(跨进程通信!)。

如何将servcie运行在另外一个进程呢?在manifest 里面配置个属性就行了。

android:process=":remote" , 代表这个service运行在同一个应用程序的不同进程中。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ckt.wangxin"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".LocalServiceTestActivity"
            android:label="@string/app_name" >
           <!--  <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter> -->
        </activity>
        <service android:name=".LocalService"></service>
        <!-- android:process=":remote" specify this service run in
        another process in the same application. -->
        <service android:name=".RemoteService" android:process=":remote"></service>
        <activity android:name="RemoteServiceTestActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

        </activity>
    </application>

</manifest>

客户端可以使用Messenger发送消息到service。

客户端代码:

package com.ckt.wangxin;

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.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.util.Log;
import android.widget.Toast;

public class RemoteServiceTestActivity extends Activity {
    static final String TAG = "RemoteServiceTestActivity";
    ServiceConnection mSc;
    public static final int SAY_HELLO_TO_CLIENT = 0;
    /**
     * Handler of incoming messages from service.
     */
    class IncomingHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case SAY_HELLO_TO_CLIENT:
                    Toast.makeText(RemoteServiceTestActivity.this.getApplicationContext(), "Hello World Remote Client!",
                            Toast.LENGTH_SHORT).show();
                    break;
                default:
                    super.handleMessage(msg);
            }
        }
    }

    Messenger messenger_reciever = new Messenger(new IncomingHandler());

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mSc = new ServiceConnection(){
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                Log.d(TAG, "service connected");
                <span style="color: rgb(204, 0, 0); ">Messenger messenger = new Messenger(service);
                Message msg = new Message();
                msg.what = RemoteService.MSG_SAY_HELLO;</span>
                msg.replyTo = messenger_reciever;
                try {
                    <span style="color: rgb(255, 0, 0); ">messenger.send(msg);</span>
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {
                Log.d(TAG, "service disconnected");
            }
        };
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d(TAG, this.getApplicationContext().getPackageCodePath());
        Intent service = new Intent(this.getApplicationContext(),RemoteService.class);
        this.bindService(service, mSc, Context.BIND_AUTO_CREATE);
    }

    @Override
    protected void onStop() {
        super.onStop();
        //must unbind the service otherwise the ServiceConnection will be leaked.
        this.unbindService(mSc);
    }
}

获得service端传来的binder,用来构建一个Messenger向service发送消息。

service端代码:

package com.ckt.wangxin;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.widget.Toast;

public class RemoteService extends Service {

    public static final int MSG_SAY_HELLO = 0;

    @Override
    public IBinder onBind(Intent intent) {
      <span style="color: rgb(204, 0, 0); ">  return messager.getBinder();</span>
    }

    Handler IncomingHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            if(msg.replyTo != null){
                Message msg_client = this.obtainMessage();
                msg_client.what = RemoteServiceTestActivity.SAY_HELLO_TO_CLIENT;
                try {
                    ((Messenger)msg.replyTo).send(msg_client);
                } catch (RemoteException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            switch (msg.what) {
                case MSG_SAY_HELLO:
                    Toast.makeText(RemoteService.this.getApplicationContext(), "Hello World Remote Service!",
                            Toast.LENGTH_SHORT).show();
                    break;
                default:
            super.handleMessage(msg);
            }
        }

    };

    Messenger  messager = new Messenger (IncomingHandler);
}

构建一个Messenger,包含一个handler,然后将messenger的binder传给客户端,客户端可以通过handler再构造一个messenger与service通信,消息在handler里面被处理。

现在是service端单向响应客户端的消息,同理可以做成双向发送消息,实现双向通信。

demo 源码可以在这里下载   源代码下载

时间: 2024-07-30 09:58:48

Android 使用binder访问service的方式的相关文章

android 多进程 Binder AIDL Service

本文参考http://blog.csdn.net/saintswordsman/article/details/5130947 android的多进程是通过Binder来实现的,一个类,继承了Binder,那么它的对象就可以被远程的进程使用了(前提是远程进程获取了这个类的对象[对象的引用],至于如如何获得看下文),怎么使用呢?在Android中, 则采用AIDL(Android InterfaceDefinition Language:接口定义语言)方式实现,所以我们必须写后缀为.aidl的文件

Kivy A to Z -- 如何从Python创建一个基于Binder的Service及如何从Java访问Python创建的Service

<Kivy A to Z -- 如何从python代码中直接访问Android的Service> 一文中讲到了如何从python访问java的service,这一篇再来讲下如何创建一个基于Binder的Python Service以及如何从Java代码中访问这个Python创建的Service. 先来看代码,再作下解释: 接<Kivy A to Z -- 如何从python代码中直接访问Android的Service>一文,我们在相关的文件中增加代码: binder_wrap.cp

Android service启动方式

1.Context.startService()方式启动 ①Context.startService()方式的生命周期: 启动时,startService –> onCreate() –> onStart()停止时,stopService –> onDestroy()如果调用者直接退出而没有停止Service,则Service 会一直在后台运行 Context.startService()方法启动服务,在服务未被创建时,系统会先调用服务的onCreate()方法,接着调用onStart(

Android Binder进程间通信---Service代理对象的获取过程

本文参考<Android系统源代码情景分析>,作者罗升阳 一.测试代码: -/Android/external/binder/server ----FregServer.cpp ~/Android/external/binder/common ----IFregService.cpp ----IFregService.h ~/Android/external/binder/client ----FregClient.cpp Binder库(libbinder)代码: ~/Android/fra

转 理解Android系统Binder机制

一.Binder机制概述 在Android开发中,很多时候我们需要用到进程间通信,所谓进程间通信,实现进程间通信的机制有很多种,比如说socket.pipe等,Android中进程间通信的方式主要有三种: 1.标准Linux Kernel IPC 接口: 2.标准D-BUS接口: 3.Binder接口. 其中,Binder机制是使用最且最被认可的,因为Binder机制有以下优点: 1.相对于其它IPC机制,Binder机制更加简洁和快速: 2.消耗的内存相对更少: 3.传统的IPC机制可能会增加

android的Binder

Binder:别针,回形针.生活中,我们用回形针将两张纸“别”在一起.android中,Binder用于进程间通信,即将两个进程“别”在一起. Binder是一种架构,有3个模块(服务端接口,Binder驱动,客服端接口),如下图: Binder服务端,实际上就是一个Binder类的对象,该对象一旦创建内部就会启动一个隐藏线程. 根据上面的架构,说说我所理解的. 客服端程序要想访问远程服务,那么它就需要获取到服务端的Binder引用---mRemote.Android工程师提供了一个解决方案,那

Android 四大组件之Service(上)

1.Service简介 Service是Android四大组件中最与Activity相似的组件,他们都代表可执行的程序.Service一直运行于后台,不会与用户交互,可用来处理一些耗时的任务(比如:后台播放音乐,I/O操作等).它的创建.配置与Activity基本相似,下面将详细介绍Android Service的开发. 2.创建.配置Service 2.1 定义一个继承Service类的子类 2.2 在AndroidManifest.xml中配置该Service 需要注意的是 Service和

Android 四大组件之 Service(二)

这里主要介绍Service组件的使用. 1.定义一个继承Service的子类 如下: package com.service; import android.app.Service; import android.content.Intent; import android.os.IBinder; public class defaultService extends Service { int mStartMode; // indicates how to behave if the serv

Android面试,与Service交互方式

五种交互方式,分别是:通过广播交互.通过共享文件交互.通过Messenger(信使)交互.通过自定义接口交互.通过AIDL交互.(可能更多) Service与Thread的区别 Thread:Thread 是程序执行的最小单元,可以用 Thread 来执行一些异步的操作. Service:Service 是android的一种机制,当它运行的时候如果是Local Service,那么对应的 Service 是运行在主进程的 main 线程上的.如果是Remote Service,那么对应的 Se