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 ...
随机推荐
- 浅析.Net数据操作机制
举个栗子,获取新闻详情的案例. public ActionResult NewsView(int newsId) { var news = _newsService.GetNewsById(newsI ...
- Kinect 开发 —— 面部追踪
SDK1.5中新增了人脸识别类库:Microsoft.Kinect.Toolkit.FaceTracking使得在Kinect中进行人脸识别变得简单,该类库的源代码也在Developer Toolki ...
- Es5正则
##JSON(ES5) 前端后台都能识别的数据.(一种数据储存格式) XNL在JSON出来前 JSON不支持 undefinde和函数. 示列:let = '[{"useername&qu ...
- 经典的横线中间文字css布局---flex布局
html: <div class="title"> <div class="line"></div> <div cla ...
- 2229: [Zjoi2011]最小割(最小割树)
Description 小白在图论课上学到了一个新的概念——最小割,下课后小白在笔记本上写下了如下这段话: “对于一个图,某个对图中结点的划分将图中所有结点分成两个部分,如果结点s,t不在同一个部分中 ...
- 【重构】C# VS 配置引用程序集的路径(分离exe和dll从指定路径调用)
原文:[重构]C# VS 配置引用程序集的路径(分离exe和dll从指定路径调用) 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/CocoWu892 ...
- Android app : use html or native?
Android app可分为两种:网络(html)应用程序和原生(native)应用程序 首先,我们先来讨论下如何判断一个app是html实现还是native实现. 设置-->>开发者选项 ...
- 如果输入的不是英文字母或者数字或者汉字,则返回false
public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_C ...
- NVM安装nodejs的方法
安装nodejs方式有很多种. 第一种:官网下载 通过nodejs官网下载安装 ,但有个缺陷,不同版本的nodejs无法顺利的切换. 第二种: NVM安装 NVM可以帮助我们快速切换 node版本 ...
- android中常见声音操作方式(Ringtone,SoundPool,MediaPlayer)小结
在Android开发中有时候需要用到播放声音操作,在android API 的media包中有三种方式可供我们选择,它们分别是Ringtone,SoundPool,MediaPlayer.因为在我目前 ...