怎么做?
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服务的更多相关文章

  1. 9.13 Binder系统_Java实现_内部机制_Server端

    logcat TestServer:* TestClient:* HelloService:* *:S &CLASSPATH=/mnt/android_fs/TestServer.jar ap ...

  2. 9.12 Binder系统_Java实现_内部机制_Client端

    Java实现中client端的RPC层(java实现)如何通过JNI来调用IPC层(C++实现)发送数据 TestServer通过addService向Service_manager注册的时候Test ...

  3. 9.9 Binder系统_Java实现_Android里java程序的编译启动

    如果知道了进程号:通过ls /proc/进程号/task 可以看到所有线程    cat /proc/进程号/task/线程号/comm  可以达到线程名字(主线程是main,主线程号就是进程号) d ...

  4. 9.2 Binder系统_驱动情景分析_服务注册过程

    1. 几个重要结构体的引入给test_server添加一个goodbye服务, 由此引入以下概念: 进程间通信其实质也是需要三要素:源.目的.数据,源是自己,目的用handle表示:通讯的过程是源向实 ...

  5. linux基础-第十单元 系统的初始化和服务

    第十单元 系统的初始化和服务 Linux系统引导的顺序 Linux系统引导的顺序 BOIS的初始化和引导加载程序 什么是BIOS GRUB程序和grub.conf文件 什么是grub grub配置文件 ...

  6. Android系统--Binder系统具体框架分析(二)Binder驱动情景分析

    Android系统--Binder系统具体框架分析(二)Binder驱动情景分析 1. Binder驱动情景分析 1.1 进程间通信三要素 源 目的:handle表示"服务",即向 ...

  7. Redis进阶实践之六Redis Desktop Manager连接Windows和Linux系统上的Redis服务

    一.引言 今天本来没有打算写这篇文章,当初我感觉使用这个工具应该很简单,下载的过程也不复杂,也没有打算记录下来.但是在使用的过程中还是出现了一些问题,为了给第一次使用Redis Desktop Man ...

  8. Redis进阶实践之六Redis Desktop Manager连接Windows和Linux系统上的Redis服务(转载6)

    Redis进阶实践之六Redis Desktop Manager连接Windows和Linux系统上的Redis服务 一.引言 今天本来没有打算写这篇文章,但是,今天测试Redis的时候发现了两个问题 ...

  9. OS X 10.10.4系统,命名为“Yosemite”(优胜美地)

    新版OS X 10.10.4系统,命名为“Yosemite”(优胜美地),拥有全新的界面设计及一些功能更新,下面一起来了解一下. 一.界面扁平化.进一步融入iOS功能 首先,OS X 10.10 Yo ...

随机推荐

  1. js防止提交数据之后的按钮连击

    js防止提交数据之后的按钮连击 一.实例描述 当页面提交的数据特别多时,页面会反应比较迟钝,此时如果用户等不及而连续单击按钮,导致数据重复提交.本案例就是为了防止数据重复提交. 二.截图 三.代码 & ...

  2. jquery插件库http://www.jq22.com/

    http://www.jq22.com/ http://www.jq22.com/jquery/%E5%8A%A0%E8%BD%BD http://www.jq22.com/jquery/%E5%BC ...

  3. #学习笔记#——JavaScript 数组部分编程(六)

    14. 题目描述 实现一个打点计时器,要求 1.从 start 到 end(包含 start 和 end),每隔 100 毫秒 console.log 一个数字,每次数字增幅为 1 2.返回的对象中需 ...

  4. LuoguP4016 负载平衡问题(费用流)

    题目描述 G 公司有 n 个沿铁路运输线环形排列的仓库,每个仓库存储的货物数量不等.如何用最少搬运量可以使 n 个仓库的库存数量相同.搬运货物时,只能在相邻的仓库之间搬运. 输入输出格式 输入格式: ...

  5. JNI/NDK开发指南(九)——JNI调用性能測试及优化

    转载请注明出处:http://blog.csdn.net/xyang81/article/details/44279725 在前面几章我们学习到了.在Java中声明一个native方法,然后生成本地接 ...

  6. 非对称算法,散列(Hash)以及证书的那些事

    转载请注明出处 http://blog.csdn.net/pony_maggie/article/details/35389657 作者:小马 这几个概念在金融电子支付领域用得比較多,我忽然认为把它们 ...

  7. 玲珑杯 Round 19 A simple math problem

    Time Limit:2s Memory Limit:128MByte Submissions:1599Solved:270 DESCRIPTION You have a sequence anan, ...

  8. python绘图问题

    论文绘图整理 # coding: utf-8 #来源:https://blog.csdn.net/A_Z666666/article/details/81165123 import matplotli ...

  9. 3/21 Django框架 模板路径及模板过滤器 1.模板路径查找

    3/21 Django框架 模板路径及模板过滤器 1.模板路径查找 先找settings.py里的TEMPLATES列表下的DIRS路径.如果APP_DIRS为True,还会到注册了的APP文件夹下依 ...

  10. 微信小程序简单常见首页demo

    wxml <view class='index-contier'> <view class="index-left"> <view>电池剩余&l ...