This article describes how to add the corresponding service access service for the JNI interface (frameworks Layer ).
In Android, hardware services generally run in an independent process to provide services for various applications. Therefore, between applications that call these hardware services and these hardware services
You must use a proxy. Therefore, we need to define the communication interface first.
1. Define communication interfaces
Go to the android-4.0.4_r1.2/frameworks/base/CORE/Java/Android/OS directory and add the itttservice. aidl file:
root@brantyou-ubuntu:~/workspace/android-4.0.4_r1.2/frameworks/base/core/java/android/os# gedit ITTTService.aidl root@brantyou-ubuntu:~/workspace/android-4.0.4_r1.2/frameworks/base/core/java/android/os#
The content is as follows:
package android.os;interface ITTTService{void setVal(int val);int getVal();}
2. Add interface file compilation Configuration
Go to the android-4.0.4_r1.2/frameworks/base Directory, open the Android. mk file, and add this line to the local_src_files variable:
core/java/android/os/ITTTService.aidl \
3. Implement Service Processing
Go to the android-4.0.4_r1.2/frameworks/base/services/Java/COM/Android/Server Directory and add the tttservice. Java file:
root@brantyou-ubuntu:~/workspace/android-4.0.4_r1.2/frameworks/base/services/java/com/android/server# gedit TTTService.java
The file content is as follows:
package com.android.server;import android.content.Context;import android.os.ITTTService;import android.util.Slog;public class TTTService extends ITTTService.Stub{private static final String TAG = "TTTService";TTTService(){init_native();}public void setVal(int val){setVal_native(val);}public int getVal(){return getVal_native();}private static native boolean init_native();private static native void setVal_native(int val);private static native int getVal_native();};
Tttservice mainly provides hardware services by calling init_native, setval_native, and getval_native in the jni method (these interfaces are provided in the previous implementation of JNI.
4. Configure to start the tttservice at startup.
Open the systemserver. Java file under the android-4.0.4_r1.2/frameworks/base/services/Java/COM/Android/Server Directory, find
Try {
Slog. I (TAG, "diskstats service ");
Servicemanager. addservice ("diskstats", new diskstatsservice (context ));
} Catch (throwable e ){
Reportwtf ("Starting diskstats service", e );
}
Add the following code to the end of this section:
try { Slog.i(TAG, "TTT Service"); ServiceManager.addService("ttt", new TTTService()); } catch (Throwable e) { // TODO: handle exception reportWtf("Failure starting TTTService", e); }
You don't have to forget to add references to this file:
import com.android.server.TTTService;
5. Start Compilation
I have already executed build/envsetup. Sh here, so I will not execute it. If this is not executed, I need to execute this file before compiling the source code.
Run the following command to compile and generate the stub interface of itttservice:
Root @ brantyou-Ubuntu :~ /Workspace/android-4.0.4_r1.2 # Mmm frameworks/base ================================ ================ response = relplatform_version = 4.0.4target _ Product = metric = engtarget_build_type = metric = target_arch = metric = armv7-aHOST_ARCH = x86host_ OS = linuxhost_build_type = releasebuild_id = imm76i ========================================================== ==== make: go to the '/home/bran' directory Tyou/workspace/android-4.0.4_r1.2 'target DEX: frameworkcopying: Out/target/common/obj/java_libraries/framework_intermediates/noproguard. classes. dextarget jar: Framework (Out/target/common/obj/java_libraries/framework_intermediates/javalib. jar) 'out/target/common/obj/java_libraries/framework_intermediates/classes. dex 'as' classes. dex '... dexpreopt boot jar: Out/target/product/generic/dex_bootjars/SY Stem/framework. odexprocessing target/product/generic/dex_bootjars/system/framework. jardone! Install: Out/target/product/generic/system/framework. odexinstall: Out/target/product/generic/system/framework. jarmake: Leave the directory "/home/brantyou/workspace/android-4.0.4_r1.2" root @ brantyou-Ubuntu :~ /Workspace/android-4.0.4_r1.2 #
Compile tttservice:
Root @ brantyou-Ubuntu :~ /Workspace/android-4.0.4_r1.2 # Mmm frameworks/base/services/Java ========================== ======================== relplatform_version = 4.0.4target _ Product = fulltarget_build_variant = engtarget_build_type = role = target_arch = Signature = armv7-aHOST_ARCH = x86host_ OS = linuxhost_build_type = releasebuild_id = imm76i ==================================== ======== make: into In the '/home/brantyou/workspace/android-4.0.4_r1.2' target Java: Services (Out/target/common/obj/java_libraries/services_intermediates/classes) Note: some input files use or overwrite outdated APIs. Note: For details, use-xlint: deprecation to re-compile. Note: Some input files use unchecked or insecure operations. Note: For details, use-xlint: unchecked to recompile. Copying: Out/target/common/obj/java_libraries/services_intermediates/classes-jarjar.jarCopying: Out/target/common/obj/java_libraries/services_intermediates/emma_out/lib/classes-jarjar.jarCopying: out/target/common/obj/java_libraries/services_intermediates/classes. jarcopying: Out/target/common/obj/java_libraries/services_intermediates/noproguard. classes. jartarget DEX: servicescopying: Out/target/COM MON/obj/java_libraries/services_intermediates/noproguard. classes. dextarget jar: Services (Out/target/common/obj/java_libraries/services_intermediates/javalib. jar) 'out/target/common/obj/java_libraries/services_intermediates/classes. dex 'as' classes. dex '... dexpreopt boot jar: Out/target/product/generic/dex_bootjars/system/framework/android. policy. odexprocessing target/product/generic/dex_bootjars/s Ystem/framework/Android. Policy. jardone! Dexpreopt boot jar: Out/target/product/generic/dex_bootjars/system/framework/services. odexprocessing target/product/generic/dex_bootjars/system/framework/services. jardone! Install: Out/target/product/generic/system/framework/services. odexinstall: Out/target/product/generic/system/framework/services. jarmake: Leave the directory "/home/brantyou/workspace/android-4.0.4_r1.2" root @ brantyou-Ubuntu :~ /Workspace/android-4.0.4_r1.2 #
Repackage system. IMG:
Root @ brantyou-Ubuntu :~ /Workspace/android-4.0.4_r1.2 # Make Snod ==================================== ============ response = relplatform_version = 4.0.4target _ Product = metric = engtarget_build_type = metric = target_arch = metric = armv7-a-neonHOST_ARCH = x86host_ OS = linuxhost_build_type = releasebuild_id = imm76i = ========================================================== = build/CORE/makef Ile: 25: Warning: overwrite the build/CORE/base_rules.mk: 523 command about the target "out/target/product/smdkv210/system/bin/pppd": Warning: ignore the old command make Snod: Ignoring dependenciestarget system FS image: Out/target/product/smdkv210/system/bin/pppd. imgout/target/product/smdkv210/system. IMG total size is 150853824root @ brantyou-Ubuntu :~ /Workspace/android-4.0.4_r1.2 #
Now, the JNI interface is accessed by the added service. Next, the android program calls the service to call the underlying interface for reading and writing.