9.10 Binder系统_Java实现_hello服务
怎么做?
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
最新文章
- quick-3.5 eclipse android
- unity5.0新功能
- http://my.oschina.net/u/2007041/blog/508520
- 无刷新分页 jquery.pagination.js
- CSS background-repeat 属性
- Android 开发笔记 “SQLite”
- LeetCode之Reverse Words in a String
- 【翻译】使用Visual Studio在Azure上部署Asp.Net Core Web应用
- C#之lambda表达式
- Python尾递归-创始人为何不愿TRE以及我们如何模拟TRE
- linux服务器部署tomcat和Nginx
- [20180327]行迁移与ITL浪费.txt
- ETL技巧应用(高级应用介绍:准备区运用、	时间戳的运用、日志表的运用、使用调度)
- LVS之NAT模型、DR模型配置和持久连接
- UVa 10618 跳舞机
- 高级功能:很有用的javascript自定义事件
- js实现瀑布流布局
- js 时间date格式化
- rtmp聊天相关归总
- mysql多表查询原理