您的位置:首页 > 编程语言 > Java开发

如何定制自己的java service: example

2016-12-03 17:59 447 查看
今天测试一个例子:
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)

看来要调试下。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: