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
9.10 Binder系统_Java实现_hello服务的更多相关文章
- 9.13 Binder系统_Java实现_内部机制_Server端
logcat TestServer:* TestClient:* HelloService:* *:S &CLASSPATH=/mnt/android_fs/TestServer.jar ap ...
- 9.12 Binder系统_Java实现_内部机制_Client端
Java实现中client端的RPC层(java实现)如何通过JNI来调用IPC层(C++实现)发送数据 TestServer通过addService向Service_manager注册的时候Test ...
- 9.9 Binder系统_Java实现_Android里java程序的编译启动
如果知道了进程号:通过ls /proc/进程号/task 可以看到所有线程 cat /proc/进程号/task/线程号/comm 可以达到线程名字(主线程是main,主线程号就是进程号) d ...
- 9.2 Binder系统_驱动情景分析_服务注册过程
1. 几个重要结构体的引入给test_server添加一个goodbye服务, 由此引入以下概念: 进程间通信其实质也是需要三要素:源.目的.数据,源是自己,目的用handle表示:通讯的过程是源向实 ...
- linux基础-第十单元 系统的初始化和服务
第十单元 系统的初始化和服务 Linux系统引导的顺序 Linux系统引导的顺序 BOIS的初始化和引导加载程序 什么是BIOS GRUB程序和grub.conf文件 什么是grub grub配置文件 ...
- Android系统--Binder系统具体框架分析(二)Binder驱动情景分析
Android系统--Binder系统具体框架分析(二)Binder驱动情景分析 1. Binder驱动情景分析 1.1 进程间通信三要素 源 目的:handle表示"服务",即向 ...
- Redis进阶实践之六Redis Desktop Manager连接Windows和Linux系统上的Redis服务
一.引言 今天本来没有打算写这篇文章,当初我感觉使用这个工具应该很简单,下载的过程也不复杂,也没有打算记录下来.但是在使用的过程中还是出现了一些问题,为了给第一次使用Redis Desktop Man ...
- Redis进阶实践之六Redis Desktop Manager连接Windows和Linux系统上的Redis服务(转载6)
Redis进阶实践之六Redis Desktop Manager连接Windows和Linux系统上的Redis服务 一.引言 今天本来没有打算写这篇文章,但是,今天测试Redis的时候发现了两个问题 ...
- OS X 10.10.4系统,命名为“Yosemite”(优胜美地)
新版OS X 10.10.4系统,命名为“Yosemite”(优胜美地),拥有全新的界面设计及一些功能更新,下面一起来了解一下. 一.界面扁平化.进一步融入iOS功能 首先,OS X 10.10 Yo ...
随机推荐
- ubuntu adb 安装
ubuntu 下adb 安装,其实就是下载一个adb,然后给它赋予可执行权限,最后在环境变量里添加一下罢了.具体如下 1.下载adb 这个工具其实是在sdk工具包里面的platform-tools文件 ...
- 值得学习的CSS知识
这里零度给大家推荐几个值得学习的CSS技巧,能让你编写网页事半功倍!一.清除默认值 通常 padding 的默认值为 0,background-color 的默认值是 transparent.但是在不 ...
- golang语言入门及安装
golang语言入门及安装 go语言是google在2009年发布的开源编程语言使用Go编译的程序可以媲美C或C++代码的速度,而且更加安全.支持并行进程. 本次讲解在windows上安装go语言的开 ...
- chkconfig---检查设置系统服务
chkconfig命令 chkconfig命令检查.设置系统的各种服务.这是Red Hat公司遵循GPL规则所开发的程序,它可查询操作系统在每一个执行等级中会执行哪些系统服务,其中包括各类常驻服务 ...
- 【习题 8-6 UVA - 1611】 Crane
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 想把数字i从位置j移动到位置i 可以这样. 假设mov(x,y)表示将(x..x+len/2-1)和(x+len/2..y)交换. ...
- Mysql学习总结(18)——Mysql主从架构的复制原理及配置详解
一.复制概述 Mysql内建的复制功能是构建大型,高性能应用程序的基础.将Mysql的数据分布到多个系统上去,这种分布的机制,是通过将Mysql的某一台主机的数据复制到其它主机(slaves)上,并重 ...
- AES与RAS结合加解密方案
import java.io.IOException; import java.security.InvalidKeyException; import java.security.KeyFactor ...
- [Anuglar & NgRx] StoreRouterConnectingModule
Always treat Router as the source of truth When we use Ngrx, we can see that we will use a "Sto ...
- hdoj 2122 Ice_cream’s world III【最小生成树】
Ice_cream's world III Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- 没有killall命令的解决方法
没有killall命令的解决方法 -bash: killall: command not found https://www.byte128.com/archives/231.html 执行killa ...