在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
目录:
一、Java程序的编译和运行 1.单步操作 PC:Hello.java 编译成 Hello.class,运行于 jvm。 javac Hello.java
java Hello
Android:Hello.java 编译成 Hello.class,通过dx指令转换成.dex格式,运行于 dalvikvm。 javac Hello.java dx --dex --output=Hello.jar Hello.class dalvikvm -cp /mnt/android_fs/pack.jar Pack (启动方法1) CLASSPATH=/mnt/android_fs/pack.jar app_process /mnt/android_fs Pack(启动方法2) 2.Android编译系统(Android.mk) 把代码放到android源码中编译: include $(CLEAR_VARS) LOCAL_SRC_FILES := $(call all-subdir-java-files) LOCAL_MODULE := pack include $(BUILD_JAVA_LIBRARY)
3.使用java代码实现hello服务 (1)IHelloService.aidl 接口定义,编译生成IHelloService.java,里面有Stub : onTransact, 它会分辨收到数据然后调用sayhello, sayhello_to; /** {@hide} */
interface IHelloService
{
void sayhello();
int sayhello_to(String name);
}
(2)IHelloService.java /* * This file is auto-generated. DO NOT MODIFY. * Original file: frameworks/base/core/java/android/os/IHelloService.aidl */ /** {@hide} */ public interface IHelloService extends android.os.IInterface { /** Local-side IPC implementation stub class. */ public static abstract class Stub extends android.os.Binder implements IHelloService { private static final java.lang.String DESCRIPTOR = "IHelloService"; /** Construct the stub at attach it to the interface. */ public Stub() { this.attachInterface(this, DESCRIPTOR); } /** * Cast an IBinder object into an IHelloService interface, * generating a proxy if needed. */ public static IHelloService asInterface(android.os.IBinder obj) { if ((obj==null)) { return null; } android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR); if (((iin!=null)&&(iin instanceof IHelloService))) { return ((IHelloService)iin); } return new IHelloService.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_sayhello: { data.enforceInterface(DESCRIPTOR); this.sayhello(); reply.writeNoException(); return true; } case TRANSACTION_sayhello_to: { data.enforceInterface(DESCRIPTOR); java.lang.String _arg0; _arg0 = data.readString(); int _result = this.sayhello_to(_arg0); reply.writeNoException(); reply.writeInt(_result); return true; } } return super.onTransact(code, data, reply, flags); } private static class Proxy implements IHelloService { 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; } @Override public void sayhello() throws android.os.RemoteException { android.os.Parcel _data = android.os.Parcel.obtain(); android.os.Parcel _reply = android.os.Parcel.obtain(); try { _data.writeInterfaceToken(DESCRIPTOR); mRemote.transact(Stub.TRANSACTION_sayhello, _data, _reply, 0); _reply.readException(); } finally { _reply.recycle(); _data.recycle(); } } @Override public int sayhello_to(java.lang.String name) 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); _data.writeString(name); mRemote.transact(Stub.TRANSACTION_sayhello_to, _data, _reply, 0); _reply.readException(); _result = _reply.readInt(); } finally { _reply.recycle(); _data.recycle(); } return _result; } } static final int TRANSACTION_sayhello = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0); static final int TRANSACTION_sayhello_to = (android.os.IBinder.FIRST_CALL_TRANSACTION + 1); } public void sayhello() throws android.os.RemoteException; public int sayhello_to(java.lang.String name) throws android.os.RemoteException; } (3)HelloService.java 实现服务类: HelloService.java,在里面定义sayhello, sayhello_to; import android.util.Slog;
/* 实现Hello服务的函数 */
public class HelloService extends IHelloService.Stub {
private static final String TAG = "HelloService";
private int cnt1 = 0;
private int cnt2 = 0;
public void sayhello() throws android.os.RemoteException {
cnt1++;
Slog.i(TAG, "sayhello : cnt = "+cnt1);
}
public int sayhello_to(java.lang.String name) throws android.os.RemoteException {
cnt2++;
Slog.i(TAG, "sayhello_to "+name+" : cnt = "+cnt2);
return cnt2;
}
}
(4)IGoodbyeService.aidl (同HelloService) /** {@hide} */
interface IGoodbyeService
{
void saygoodbye();
int saygoodbye_to(String name);
}
(5)IGoodbyeService.java /*
* This file is auto-generated. DO NOT MODIFY.
* Original file: frameworks/base/core/java/android/os/IGoodbyeService.aidl
*/
/** {@hide} */
public interface IGoodbyeService extends android.os.IInterface
{
/** Local-side IPC implementation stub class. */
public static abstract class Stub extends android.os.Binder implements IGoodbyeService
{
private static final java.lang.String DESCRIPTOR = "IGoodbyeService";
/** Construct the stub at attach it to the interface. */
public Stub()
{
this.attachInterface(this, DESCRIPTOR);
}
/**
* Cast an IBinder object into an IGoodbyeService interface,
* generating a proxy if needed.
*/
public static IGoodbyeService asInterface(android.os.IBinder obj)
{
if ((obj==null)) {
return null;
}
android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
if (((iin!=null)&&(iin instanceof IGoodbyeService))) {
return ((IGoodbyeService)iin);
}
return new IGoodbyeService.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_saygoodbye:
{
data.enforceInterface(DESCRIPTOR);
this.saygoodbye();
reply.writeNoException();
return true;
}
case TRANSACTION_saygoodbye_to:
{
data.enforceInterface(DESCRIPTOR);
java.lang.String _arg0;
_arg0 = data.readString();
int _result = this.saygoodbye_to(_arg0);
reply.writeNoException();
reply.writeInt(_result);
return true;
}
}
return super.onTransact(code, data, reply, flags);
}
private static class Proxy implements IGoodbyeService
{
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;
}
@Override public void saygoodbye() throws android.os.RemoteException
{
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
try {
_data.writeInterfaceToken(DESCRIPTOR);
mRemote.transact(Stub.TRANSACTION_saygoodbye, _data, _reply, 0);
_reply.readException();
}
finally {
_reply.recycle();
_data.recycle();
}
}
@Override public int saygoodbye_to(java.lang.String name) 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);
_data.writeString(name);
mRemote.transact(Stub.TRANSACTION_saygoodbye_to, _data, _reply, 0);
_reply.readException();
_result = _reply.readInt();
}
finally {
_reply.recycle();
_data.recycle();
}
return _result;
}
}
static final int TRANSACTION_saygoodbye = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
static final int TRANSACTION_saygoodbye_to = (android.os.IBinder.FIRST_CALL_TRANSACTION + 1);
}
public void saygoodbye() throws android.os.RemoteException;
public int saygoodbye_to(java.lang.String name) throws android.os.RemoteException;
}
(6)GoodbyeService.java import android.util.Slog; /* 实现Goodbye服务的函数 */ public class GoodbyeService extends IGoodbyeService.Stub { private static final String TAG = "GoodbyeService"; private int cnt1 = 0; private int cnt2 = 0; public void saygoodbye() throws android.os.RemoteException { cnt1++; Slog.i(TAG, "saygoodbye : cnt = "+cnt1); } public int saygoodbye_to(java.lang.String name) throws android.os.RemoteException { cnt2++; Slog.i(TAG, "saygoodbye_to "+name+" : cnt = "+cnt2); return cnt2; } }
(7)TestServer.java (addService, 循环) import android.util.Slog;
import android.os.ServiceManager;
/* 1. addService
* 2. while(true) { read data, parse data, call function, reply }
*/
public class TestServer {
private static final String TAG = "TestServer";
public static void main(String args[])
{
/* add Service */
Slog.i(TAG, "add hello service");
ServiceManager.addService("hello", new HelloService());
Slog.i(TAG, "add goodbye service");
ServiceManager.addService("goodbye", new GoodbyeService());
while (true)
{
try {
Thread.sleep(100);
} catch (Exception e){}
}
}
}
(8)TestClient.java (getService, 调用sayhello,sayhello_to(来自Proxy)) import android.util.Slog; import android.os.ServiceManager; import android.os.IBinder; /* 1. getService * 2. 调用服务的sayhello,sayhello_to * */ /* test_client <hello|goodbye> [name] */ public class TestClient { private static final String TAG = "TestClient"; public static void main(String args[]) { if (args.length == 0) { System.out.println("Usage: need parameter: <hello|goodbye> [name]"); return; } if (args[0].equals("hello")) { /* 1. getService */ IBinder binder = ServiceManager.getService("hello"); if (binder == null) { System.out.println("can not get hello service"); Slog.i(TAG, "can not get hello service"); return; } IHelloService svr = IHelloService.Stub.asInterface(binder); if (args.length == 1) { try { svr.sayhello(); System.out.println("call sayhello"); Slog.i(TAG, "call sayhello"); } catch (Exception e) {} } else { try { int cnt = svr.sayhello_to(args[1]); System.out.println("call sayhello_to "+args[1]+" : cnt = "+cnt); Slog.i(TAG, "call sayhello_to "+args[1]+" : cnt = "+cnt); } catch (Exception e) { System.out.println("call sayhello_to , err :"+e); Slog.i(TAG, "call sayhello_to , err : "+e); } } } else if (args[0].equals("goodbye")) { /* 1. getService */ IBinder binder = ServiceManager.getService("goodbye"); if (binder == null) { System.out.println("can not get goodbye service"); Slog.i(TAG, "can not get goodbye service"); return; } IGoodbyeService svr = IGoodbyeService.Stub.asInterface(binder); if (args.length == 1) { try { svr.saygoodbye(); System.out.println("call saygoodbye"); Slog.i(TAG, "call saygoodbye"); } catch (Exception e) {} } else { try { int cnt = svr.saygoodbye_to(args[1]); System.out.println("call saygoodbye_to "+args[1]+" : cnt = "+cnt); Slog.i(TAG, "call saygoodbye_to "+args[1]+" : cnt = "+cnt); } catch (Exception e) {} } } } }
(9)Android.mk # Copyright 2008 The Android Open Source Project # LOCAL_PATH:= $(call my-dir) include $(CLEAR_VARS) LOCAL_SRC_FILES := HelloService.java IHelloService.java GoodbyeService.java IGoodbyeService.java TestServer.java LOCAL_MODULE := TestServer include $(BUILD_JAVA_LIBRARY) include $(CLEAR_VARS) LOCAL_SRC_FILES := HelloService.java IHelloService.java GoodbyeService.java IGoodbyeService.java TestClient.java LOCAL_MODULE := TestClient include $(BUILD_JAVA_LIBRARY)
编译&测试说明: (1) AIDL
1. 把 IHelloService.aidl, IGoodbyeService.aidl 放入 frameworks/base/core/java/android/os
2. 修改 frameworks/base/Android.mk 添加一行
core/java/android/os/IVibratorService.aidl \
+ core/java/android/os/IHelloService.aidl \
+ core/java/android/os/IGoodbyeService.aidl \
3. mmm frameworks/base
4. 它会生成:
./out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/src/core/java/android/os/IHelloService.java
./out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/src/core/java/android/os/IGoodbyeService.java
(2) 编译:
把程序放到 /work/android-5.0.2/frameworks/testing/APP_0005_Binder_JAVA_App
执行:
cd /work/android-5.0.2
. setenv
lunch // 选择单板
mmm frameworks/testing/APP_0005_Binder_JAVA_App
它会生成 TestServer.jar, TestClient.jar
复制文件到NFS目录
cp /work/android-5.0.2/out/target/product/tiny4412/system/framework/Test*.jar /work/nfs_root/android_fs/
(3) 测试:
logcat TestServer:* TestClient:* HelloService:* *:S &
CLASSPATH=/mnt/android_fs/TestServer.jar app_process / TestServer &
CLASSPATH=/mnt/android_fs/TestClient.jar app_process / TestClient hello
CLASSPATH=/mnt/android_fs/TestClient.jar app_process / TestClient hello 100ask.taobao.com
CLASSPATH=/mnt/android_fs/TestClient.jar app_process / TestClient goodbye
CLASSPATH=/mnt/android_fs/TestClient.jar app_process / TestClient goodbye weidongshan
二、内部机制 1.Client端
2.Server端 2.1 server如何读取数据 /*测试指令*/ 它会先创建子线程(前面示例提到的两个binder线程:Binder_1, Binder_2):
-end- |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论