天天看點

05.Binder系統:第8課第3節_Binder系統_JAVA實作_hello服務_測試

下面的是上小節編寫的程式,經過修改并且編譯成功能正常運作的代碼:

IHelloService.aidl

/** {@hide} */
interface IHelloService
{
	void sayhello();
	int sayhello_to(String name);
}
           

HelloService.java

import android.util.Slog;

/* 實作Hello服務的函數 */

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;
    }
}


           

TestServer.java

import android.util.Slog;
import android.os.ServiceManager;

/* 1. addService
 * 2. while(true) { read data, parse data, call function, reply }
 */

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)
        {
            try {
            	Thread.sleep(100);
          	} catch (Exception e){}
        }
        
    }
}
 

           

TestClient.java

import android.util.Slog;
import android.os.ServiceManager;
import android.os.IBinder;


/* 1. getService
 * 2. 調用服務的sayhello,sayhello_to
 *
 */

/* test_client <hello|goodbye> [name] */

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|goodbye> [name]");
            return;
        }

        if (args[0].equals("hello"))
        {
            /* 1. 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 svr = IHelloService.Stub.asInterface(binder);

            if (args.length == 1)
            {
            		try {
	                svr.sayhello();
	                System.out.println("call sayhello");
	                Slog.i(TAG, "call sayhello");
              	} catch (Exception e) {}
            }
            else
            {
            		try {
	                int cnt = svr.sayhello_to(args[1]);
	                System.out.println("call sayhello_to "+args[1]+" : cnt = "+cnt);
	                Slog.i(TAG, "call sayhello_to "+args[1]+" : cnt = "+cnt);
              	} catch (Exception e) {}
            }
        }
    }
}


           

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)
           

編譯測試

把上述檔案全部編寫完成之後,所有檔案都放在同一檔案夾中,然後把該檔案夾拷貝到frameworks\testing(如果沒有,請自行建立)目錄下,執行mmm指令,可以看到生産兩個檔案:

out/target/product/qytech_azalea/system/framework/TestServer.jar

out/target/product/qytech_azalea/system/framework/TestClient.jar

把這兩個檔案通過abd下載下傳到開發闆上執行:

logcat TestServer:* TestClient:* HelloService:* *:S &

CLASSPATH=./TestServer.jar app_process ./ TestServer &

CLASSPATH=./TestClient.jar app_process ./ TestClient hello

CLASSPATH=./TestClient.jar app_process ./ TestClient hello weidongshan

繼續閱讀