如何定制自己的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();
}
}
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
adb shell
root@:/ # /system/bin/ServerDemo
Aborted (core dumped)
窗口2:
C:\>adb shell
root@:/ # /system/bin/ClientDemo
Aborted (core dumped)
看来要调试下。
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)
看来要调试下。
相关文章推荐
- 如何定制自己的native service: example (C++)
- 如何定制自己的native service: example2 (C++)
- Java-如何定制自己的工具库: mytools or myutil
- 如何用java按需定制自己的迭代器
- 服务器如何架设自己开发的Service(一)
- 服务器如何架设自己开发的Service(二)
- Android 非运营商定制项目如何客制化自己的开关机动画、开关机铃声
- DELPHI7如何调用带参数的JAVA WebService
- delphi客户端调服务器端的java webservice如何在参数中传对象? 转
- java 如何创建自己的库
- 如何定制你自己的jQuery
- 在Java中如何实现自己的annotation
- 2.如何用好ASSERT宏让ASSERT更好用(定制自己的Assert)
- (android)如何创建自己可用的Service类
- 如何提高自己的 Java 编程技能
- java如何定义自己的类
- JAVA CDI 学习(5) - 如何向RESTFul Service中注入EJB实例
- 如何自己定制iOS框架(framework)
- 如何建立自己的Java库?
- Eclipse环境下 java如何调第三方jar包或自己封装好的工具类