java开发Binder服务

Binder使用简单,开发一个Binder服务就相对复杂,相比较而言,java开发一个服务比C++要简单的多。
下面写一个比较简单的组件Service,它里面包含了Binder服务。
(1)第一步,编写aidl文件,在java包目录下创建一个aidl文件(IExampleService.aidl),并且根据需求声明方法,然后build一下,内容如下:

// IExampleService.aidl
package com.hodi.hodi_opencv;
import statements

interface IExampleService {

     int get();
      void set(int value);
}

build后生成IExampleService.java,内容如下:

/*
 * This file is auto-generated.  DO NOT MODIFY.
 * Original file: E:\\work\\Andriod\\Hodi_Opencv\\app\\src\\main\\aidl\\com\\hodi\\hodi_opencv\\IExampleService.aidl
 */
package com.hodi.hodi_opencv;
// Declare any non-default types here with import statements

public interface IExampleService extends android.os.IInterface
{
/** Local-side IPC implementation stub class. */
public static abstract class Stub extends android.os.Binder implements com.hodi.hodi_opencv.IExampleService
{
private static final java.lang.String DESCRIPTOR = "com.hodi.hodi_opencv.IExampleService";
/** Construct the stub at attach it to the interface. */
public Stub()
{
this.attachInterface(this, DESCRIPTOR);
}
/**
 * Cast an IBinder object into an com.hodi.hodi_opencv.IExampleService interface,
 * generating a proxy if needed.
 */
public static com.hodi.hodi_opencv.IExampleService asInterface(android.os.IBinder obj)
{
if ((obj==null)) {
return null;
}
android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
if (((iin!=null)&&(iin instanceof com.hodi.hodi_opencv.IExampleService))) {
return ((com.hodi.hodi_opencv.IExampleService)iin);
}
return new com.hodi.hodi_opencv.IExampleService.Stub.Proxy(obj);
}
@Override public android.os.IBinder asBinder()
{
return this;
}
@Override public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) throws android.os.RemoteException
{
switch (code)
{
case INTERFACE_TRANSACTION:
{
reply.writeString(DESCRIPTOR);
return true;
}
case TRANSACTION_get:
{
data.enforceInterface(DESCRIPTOR);
int _result = this.get();
reply.writeNoException();
reply.writeInt(_result);
return true;
}
case TRANSACTION_set:
{
data.enforceInterface(DESCRIPTOR);
int _arg0;
_arg0 = data.readInt();
this.set(_arg0);
reply.writeNoException();
return true;
}
}
return super.onTransact(code, data, reply, flags);
}
private static class Proxy implements com.hodi.hodi_opencv.IExampleService
{
private android.os.IBinder mRemote;
Proxy(android.os.IBinder remote)
{
mRemote = remote;
}
@Override public android.os.IBinder asBinder()
{
return mRemote;
}
public java.lang.String getInterfaceDescriptor()
{
return DESCRIPTOR;
}
/**
     * Demonstrates some basic types that you can use as parameters
     * and return values in AIDL.
     *///    void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
//            double aDouble, String aString);

@Override public int get() throws android.os.RemoteException
{
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
int _result;
try {
_data.writeInterfaceToken(DESCRIPTOR);
mRemote.transact(Stub.TRANSACTION_get, _data, _reply, 0);
_reply.readException();
_result = _reply.readInt();
}
finally {
_reply.recycle();
_data.recycle();
}
return _result;
}
@Override public void set(int value) throws android.os.RemoteException
{
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
try {
_data.writeInterfaceToken(DESCRIPTOR);
_data.writeInt(value);
mRemote.transact(Stub.TRANSACTION_set, _data, _reply, 0);
_reply.readException();
}
finally {
_reply.recycle();
_data.recycle();
}
}
}
static final int TRANSACTION_get = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
static final int TRANSACTION_set = (android.os.IBinder.FIRST_CALL_TRANSACTION + 1);
}
/**
     * Demonstrates some basic types that you can use as parameters
     * and return values in AIDL.
     *///    void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
//            double aDouble, String aString);

public int get() throws android.os.RemoteException;
public void set(int value) throws android.os.RemoteException;
}

整体上来看,IExampleService.java文件中的IExampleService类有一个Stub的内部类,还有一个get()和一个set()方法。这个方法就是我们在aidl中定义的方法。同时我们看到,生成的IExampleService继承自IInterface,说明这也是一个接口,并没有对get()和set()方法实现。

(2)第二步,编写Service的代码:

package com.hodi.hodi_opencv;


import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;

import java.security.Provider;

/**
 * Created by AA on 2016/11/24.
 */

public  class ExampleService extends Service {
    private int mValue=0;
    private final IExampleService.Stub mInstance  = new IExampleService.Stub(){
        public int get(){
            return mValue;
        }
        public void set(int value){
            mValue = value;
        }
    };

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return mInstance;
    }
}

ExampleService继承Service,而且必须重载底层onBind()方法,并在其中返回Binder服务的实体对象mInstance。IExampleService.Stub 类是真正的Binder服务类,它是通过前面的aidl生成的。

(3)第三步,在AndroidManifest.xml中加入了服务的声明:

<service android:name=".ExampleService">
            <intent-filter>
                <action android:name="com.hodi.hodi_opencv.IExampleService"/>
            </intent-filter>
        </service>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值