怎么做?
2.1 定义接口:
写IHelloService.aidl文件, 上传, 编译, 得到IHelloService.java
里面有Stub : onTransact, 它会分辨收到数据然后调用sayhello, sayhello_to
有Proxy : 提供有sayhello, sayhello_to两个函数, 它们会构造数据然后发送给server
(这样我们就不用像C++那样实现BnHelloService和BpHelloService了)
2.2 实现服务类: HelloService.java
在里面定义sayhello, sayhello_to
2.3 实现APP: TestServer, TestClient
TestServer : addService, 循环
TestCliet : getService, 调用sayhello,sayhello_to(来自Proxy)
3. 用java实现hello服务_测试
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 weidongshan
IHelloService.aidl
/** {@hide}*/
interface IHelloService
{
void sayhello();
int sayhello_to(String name);
}
把IHelloService.aidl放在已经编译好的Android系统源码中的目录:frameworks/base/core/java/android/os目录下,同时修改frameworks/base下的Android.mk文件,其就是makefile文件,其他子目录没有Android.mk文件:仿照其他添加aidl文件添加一句:
core/java/android/os/IHelloService.aidl
接着在frameworks/base目录下执行:mmm . (该命令会帮我们生成IHelloService.java文件,同时执行mmm命令的前提是已经成功编译了Android系统)
编译的结果会放到out目录下,进入out目录下搜索:“find -name "IHelloService.java”
.out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/src/core/java/android/os目录下有个IHelloService.java文件
HelloService.java
/*实现Hello服务的函数*/
import android.util.Slog;
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;
}
}
Test_Server.java
/*1、addservice 2、while(true){read data,parse data,call function,reply}*/
import android.util.Slog;
import android.os.ServiceManager;
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())
while(true)
{
//应用程序运行的时候使用app_process启动,这个app_process也是一个应用程序,其会创建两个线程binder_1和binder_2来接受数据、处理数据设置返回值
try{
Thread.sleep(100);
}catch (Exception e){}
}
}
}
Test_Client.java
/*1、getservice 2、调用服务的函数*/
import android.util.Slog;
import android.os.ServiceManager;
import android.os.IBinder;
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|goodbyte> [name]");
return;
}
if(args[0].equals("hello"))
{
/*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 srv = IHelloService.Stub.asInterface(binder );
if(args.length == 1)
{
try{
srv .sayhello();
System.out.println("call sayhello");
Slog.i(TAG,"call sayhello");
}catch (Exception e){}
}
else
{
try{
int cnt = srv.sayhello_to(args[1]);
System.out.println("call sayhello"+args[1]+":cnt = "+cnt);
Slog.i(TAG,"call sayhello"+args[1]+":cnt = "+cnt);
}catch (Exception e){}
}
}
}
}
添加Androd.mk,内容类似: // 参考frameworks/base/cmds/am/Android.mk
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES :=HelloService.java IHelloService.java TestServer.java
LOCAL_MODULE := TestServer
include $(BUILD_JAVA_LIBRARY)
include $(CLEAR_VARS)
LOCAL_SRC_FILES :=HelloService.java IHelloService.java TestClient.java
LOCAL_MODULE := TestServer
include $(BUILD_JAVA_LIBRARY)
mmm执行后生成TestClient.jar和TestServer.jar