如何定制自己的java service: example


今天测试一个例子:

https://github.com/yuanhuihui/BinderSample

有问题!

Server侧:

IMyService.java:

package com.test.frameworkBinder;

import android.os.IInterface;
import android.os.RemoteException;

public interface IMyService extends IInterface {
    static final java.lang.String DESCRIPTOR = "com.test.frameworkBinder.MyServer";
    public void sayHello(String str) throws RemoteException;
    static final int TRANSACTION_say = android.os.IBinder.FIRST_CALL_TRANSACTION;
}

MyService.java:

package com.test.frameworkBinder;

import android.os.Binder;
import android.os.IBinder;
import android.os.Parcel;
import android.os.RemoteException;

public class MyService extends Binder implements IMyService {
    public MyService() {
        this.attachInterface(this, DESCRIPTOR);
    }

    @Override
    public IBinder asBinder() {
        return this;
    }
 
    // convert MyService to IMyService Interface
    public static com.test.frameworkBinder.IMyService asInterface(
            android.os.IBinder obj) {
        if ((obj == null)) {
            return null;
        }

        android.os.IInterface iInterface = obj.queryLocalInterface(DESCRIPTOR);
        if (((iInterface != null) && (iInterface instanceof com.test.frameworkBinder.IMyService))){
            return ((com.test.frameworkBinder.IMyService) iInterface);
        }
        return null;    
    }

    // Server side: receive remote message, and deal with OnTransact()
    @Override
    protected boolean onTransact(int code, Parcel data, Parcel reply, int flags)
            throws RemoteException {
            switch (code) {
                case INTERFACE_TRANSACTION: 
                    reply.writeString(DESCRIPTOR);
                    return true;

                case TRANSACTION_say:
                    data.enforceInterface(DESCRIPTOR);
                    String str = data.readString();
                    sayHello(str);
                    reply.writeNoException();
                    return true;
            } 
            return super.onTransact(code, data, reply, flags);
    }

    // Server side's sayHello()
    @Override
    public void sayHello(String str) {
        System.out.println("MyService:: Hello, " + str);
    }

}

ServerDemo.java:

package com.test.frameworkBinder;

import android.os.Looper;
import android.os.ServiceManager;

public class ServerDemo {
    public static void main (String[] args) {
        System.out.println("MyService start");

        Looper.prepareMainLooper();

        android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_FOREGROUND);

        ServiceManager.addService("MyService", new MyService());

        Looper.loop();
    }
}

Client 侧:

IMyservice.java:

package com.test.frameworkBinder;

import android.os.IInterface;
import android.os.RemoteException;

public interface IMyService extends IInterface {
    static final java.lang.String DESCRIPTOR = "com.test.frameworkBinder.MyServer";
    public void sayHello(String str) throws RemoteException;
    static final int TRANSACTION_say = android.os.IBinder.FIRST_CALL_TRANSACTION;
}

MyServiceProxy.java:

package com.test.frameworkBinder;

import android.os.IBinder;
import android.os.RemoteException;

public class MyServiceProxy implements IMyService {
    private android.os.IBinder mRemote;

    public MyServiceProxy(android.os.IBinder remote) {
        mRemote = remote;
    }

    public java.lang.String getInterfaceDescriptor() {
        return DESCRIPTOR;
    }

    @Override
    public void sayHello(String str) throws RemoteException {
        android.os.Parcel _data = android.os.Parcel.obtain();
        android.os.Parcel _reply = android.os.Parcel.obtain();

        try {
            _data.writeInterfaceToken(DESCRIPTOR);
            _data.writeString(str);
            mRemote.transact(TRANSACTION_say, _data, _reply, 0);
            _reply.readException();
        } finally {
            _reply.recycle();
            _data.recycle();
        }
    }

    @Override 
    public IBinder asBinder() {
        return mRemote;
    }
}

ClientDemo.java:

package com.test.frameworkBinder;

import android.os.IBinder;
import android.os.RemoteException;
import android.os.ServiceManager;

public class ClientDemo {

    public static void main(String[] args) throws RemoteException {
        System.out.println ("Client start");
        IBinder binder = ServiceManager.getService("MyService");
        IMyService myService = new MyServiceProxy(binder);
        myService.sayHello("Binder");
        System.out.println("Client end");
    }
}

编译完成后:

adb push ServerDemo /system/bin
adb push ClientDemo /system/bin
adb push ServerDemo.jar /system/framework
adb push ClientDemo.jar /system/framework

运行:

窗口1:

adb shell
root@:/ # /system/bin/ServerDemo
Aborted (core dumped)

窗口2:

C:\>adb shell
root@:/ # /system/bin/ClientDemo
Aborted (core dumped)

看来要调试下。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值