java编写binder服务实例

文件目录结果如下:

一、 编写AIDL文件

IHelloService.aidl:

1 /** {@hide} */
2 interface IHelloService
3 {
4     void sayhello();
5     int sayhello_to(String name);
6 }
View Code

1. 把 IHelloService.aidl 放入 frameworks/base/core/java/android/os
2. 修改 frameworks/base/Android.mk 添加一行
core/java/android/os/IVibratorService.aidl \
+ core/java/android/os/IHelloService.aidl \

3. mmm frameworks/base

4. 它会生成: ./out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/src/core/java/android/os/IHelloService.java

IHelloService.java自动生成,无需修改

  1 /*
  2  * This file is auto-generated.  DO NOT MODIFY.
  3  * Original file: frameworks/base/core/java/android/os/IHelloService.aidl
  4  */
  5 /** {@hide} */
  6 public interface IHelloService extends android.os.IInterface
  7 {
  8     /** Local-side IPC implementation stub class. */
  9     public static abstract class Stub extends android.os.Binder implements IHelloService
 10     {
 11         private static final java.lang.String DESCRIPTOR = "IHelloService";
 12         /** Construct the stub at attach it to the interface. */
 13         public Stub()
 14         {
 15             this.attachInterface(this, DESCRIPTOR);
 16         }
 17         /**
 18          * Cast an IBinder object into an IHelloService interface,
 19          * generating a proxy if needed.
 20          */
 21         public static IHelloService asInterface(android.os.IBinder obj)
 22         {
 23             if ((obj==null)) {
 24             return null;
 25             }
 26             android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
 27             if (((iin!=null)&&(iin instanceof IHelloService))) {
 28             return ((IHelloService)iin);
 29             }
 30             return new IHelloService.Stub.Proxy(obj);
 31         }
 32         
 33         @Override public android.os.IBinder asBinder()
 34         {
 35             return this;
 36         }
 37         @Override public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) throws android.os.RemoteException
 38         {
 39             switch (code)
 40             {
 41                 case INTERFACE_TRANSACTION:
 42                 {
 43                     reply.writeString(DESCRIPTOR);
 44                     return true;
 45                 }
 46                 case TRANSACTION_sayhello:
 47                 {
 48                     data.enforceInterface(DESCRIPTOR);
 49                     this.sayhello();
 50                     reply.writeNoException();
 51                     return true;
 52                 }
 53                 case TRANSACTION_sayhello_to:
 54                 {
 55                     data.enforceInterface(DESCRIPTOR);
 56                     java.lang.String _arg0;
 57                     _arg0 = data.readString();
 58                     int _result = this.sayhello_to(_arg0);
 59                     reply.writeNoException();
 60                     reply.writeInt(_result);
 61                     return true;
 62                 }
 63             }
 64             return super.onTransact(code, data, reply, flags);
 65         }
 66 
 67         
 68         private static class Proxy implements IHelloService
 69         {
 70             private android.os.IBinder mRemote;
 71             Proxy(android.os.IBinder remote)
 72             {
 73                 mRemote = remote;
 74             }
 75             @Override public android.os.IBinder asBinder()
 76             {
 77                 return mRemote;
 78             }
 79             public java.lang.String getInterfaceDescriptor()
 80             {
 81                 return DESCRIPTOR;
 82             }
 83             @Override public void sayhello() throws android.os.RemoteException
 84             {
 85                 android.os.Parcel _data = android.os.Parcel.obtain();
 86                 android.os.Parcel _reply = android.os.Parcel.obtain();
 87                 try {
 88                     _data.writeInterfaceToken(DESCRIPTOR);
 89                     mRemote.transact(Stub.TRANSACTION_sayhello, _data, _reply, 0);
 90                     _reply.readException();
 91                 }
 92                 finally {
 93                     _reply.recycle();
 94                     _data.recycle();
 95                 }
 96             }
 97             @Override public int sayhello_to(java.lang.String name) throws android.os.RemoteException
 98             {
 99                 android.os.Parcel _data = android.os.Parcel.obtain();
100                 android.os.Parcel _reply = android.os.Parcel.obtain();
101                 int _result;
102                 try {
103                     _data.writeInterfaceToken(DESCRIPTOR);
104                     _data.writeString(name);
105                     mRemote.transact(Stub.TRANSACTION_sayhello_to, _data, _reply, 0);
106                     _reply.readException();
107                     _result = _reply.readInt();
108                 }
109                 finally {
110                     _reply.recycle();
111                     _data.recycle();
112                 }
113                     return _result;
114             }
115         }
116         
117         static final int TRANSACTION_sayhello = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
118         static final int TRANSACTION_sayhello_to = (android.os.IBinder.FIRST_CALL_TRANSACTION + 1);
119             
120     }
121     
122     public void sayhello() throws android.os.RemoteException;
123     public int sayhello_to(java.lang.String name) throws android.os.RemoteException;
124 }
View Code

HelloService.java

 1 import android.util.Slog;
 2 
 3 /* 实现Hello服务的函数 */
 4 
 5 public class HelloService extends IHelloService.Stub {
 6     private static final String TAG = "HelloService";
 7     private int cnt1 = 0;
 8     private int cnt2 = 0;
 9 
10     public void sayhello() throws android.os.RemoteException {
11         cnt1++;
12         Slog.i(TAG, "sayhello : cnt = "+cnt1);
13     }
14     
15     public int sayhello_to(java.lang.String name) throws android.os.RemoteException {
16         cnt2++;
17         Slog.i(TAG, "sayhello_to "+name+" : cnt = "+cnt2);
18         return cnt2;
19     }
20 }
View Code

TestServer.java 服务端测试程序

 1 import android.util.Slog;
 2 import android.os.ServiceManager;
 3 
 4 /* 1. addService
 5  * 2. while(true) { read data, parse data, call function, reply }
 6  */
 7 
 8 public class TestServer {
 9     private static final String TAG = "TestServer";
10 
11     public static void main(String args[])
12     {
13         /* add Service */
14         Slog.i(TAG, "add hello service");
15         ServiceManager.addService("hello", new HelloService());
16 
17         while (true)
18         {
19             try {
20                 Thread.sleep(100);
21               } catch (Exception e){}
22         }
23         
24     }
25 }
26  
View Code

TestClient.java 客户端测试程序

 1 import android.util.Slog;
 2 import android.os.ServiceManager;
 3 import android.os.IBinder;
 4 
 5 /* 1. getService
 6  * 2. 调用服务的sayhello,sayhello_to
 7  *
 8  */
 9 
10 /* test_client <hello|goodbye> [name] */
11 
12 public class TestClient {
13     private static final String TAG = "TestClient";
14 
15     public static void main(String args[])
16     {
17         if (args.length == 0)
18         {
19             System.out.println("Usage: need parameter: <hello|goodbye> [name]");
20             return;
21         }
22 
23         if (args[0].equals("hello"))
24         {
25             /* 1. getService */
26             IBinder binder = ServiceManager.getService("hello");
27             if (binder == null)
28             {
29                 System.out.println("can not get hello service");
30                 Slog.i(TAG, "can not get hello service");
31                 return;
32             }
33 
34             IHelloService svr = IHelloService.Stub.asInterface(binder);
35 
36             if (args.length == 1)
37             {
38                     try {
39                     svr.sayhello();
40                     System.out.println("call sayhello");
41                     Slog.i(TAG, "call sayhello");
42                   } catch (Exception e) {}
43             }
44             else
45             {
46                     try {
47                     int cnt = svr.sayhello_to(args[1]);
48                     System.out.println("call sayhello_to "+args[1]+" : cnt = "+cnt);
49                     Slog.i(TAG, "call sayhello_to "+args[1]+" : cnt = "+cnt);
50                   } catch (Exception e) {}
51             }
52         }
53     }
54 }
View Code

Android.mk文件

# Copyright 2008 The Android Open Source Project
#
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 := TestClient
include $(BUILD_JAVA_LIBRARY)

二、编译:
把程序放到 /work/android-5.0.2/frameworks/testing/APP_Binder_JAVA_App
执行:

. setenv
lunch // 选择单板

mmm frameworks/testing/APP_Binder_JAVA_App
在out目录下它会生成 TestServer.jar, TestClient.jar

(3) 测试:
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 winfu

 

posted @ 2017-09-14 16:33  winfu  阅读(956)  评论(0编辑  收藏  举报