Add a framework interface for the application, that is, the SDK API
Create the PMPS directory in the framework/base Directory, create the \ Java \ Android \ PMPS \ pmpsmanager. Java file in the PMPS directory, and enter the code:
Package Android. PMPS;
Import Android. util. log;
Public class pmpsmanager
{
Private Static final string log_tag = "pmpsmanager ";
Static {
System. loadlibrary ("pmps_jni ");
}
Public pmpsmanager (){
Log. I (log_tag, "pmpsmanager Constructor ().");
}
Public native static void sayhello ();
}
Pmpsmanager provides related APIs for the application layer, such as sayhello (). Here, the native method of jni is called directly. It is worth noting that the system. loadlibrary ("pmps_jni") file loaded by pmpsmanager loads the libpmps_jni.so file that will be generated in step 1 of the article.
For more information, see the Android. mk file in the framework/base directory:
# Frameworks_base_subdirs comes from build/CORE/pathmap. mk
Local_src_files: = $ (call find-other-Java-files, $ (frameworks_base_subdirs ))
Let's take a look at build/CORE/pathmap. mk, where there is a section
Frameworks_base_subdirs: = \
$ (Addsuffix/Java ,\
Core \
Graphics \
Location \
Media \
OpenGL \
Sax \
Telephony \
WiFi \
VPN \
Keystore \
VoIP \
)
In other words, we need to add the PMPS directory to the build/CORE/pathmap. mk file. After adding the directory:
Frameworks_base_subdirs: = \
$ (Addsuffix/Java ,\
Core \
Graphics \
Location \
Media \
OpenGL \
Sax \
Telephony \
WiFi \
VPN \
Keystore \
VoIP \
PMPS \
)
Then we can use the mmm command to compile the newly added module.
Mmm framework/base compilation. The generated file is out/target/product/generic/system/framework. jar.
After the code is compiled, you can do the following.
2. Framework calls native code, that is, JNI
Publicnative static void sayhello () in the previous step can be called by the application layer. Of course, we can encapsulate it in the framework to implement more complex functions, for example:
// API
Public int sayhellofirst ()
{
// Omitted code...
Return sayhello ();
}
// Declaration
Public native static void sayhello ();
3. Some JNI implementation functions
In this step, you can write native code to implement the calling function in the framework, or call the relevant drivers at the Hal layer to complete the hardware operation function.
At present, the information is simply printed.
It is worth noting that to implement the jni_onload function, you can also rewrite the jni_onunload function if necessary.
/* // Device/libs/android_runtime/android_media_mediaplayer.cpp
**
** Copyright 2007, the android open source project
**
** Licensed under theapache license, version 2.0 (the "License ");
** You may not usethis file has t in compliance with the license.
** You may obtain acopy of the license
**
** Http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required byapplicable law or agreed to in writing, software
** Distributed underthe license is distributed on an "as is" basis,
** Without warrantiesor conditions of any kind, either express or implied.
** See the licensefor the specific language governing permissions and
** Limitations underthe license.
*/
# Define log_ndebug 0
# Define log_tag "pmpsmanager-JNI"
# Include "utils/log. H"
# Include <stdio. h>
# Include <assert. h>
# Include <limits. h>
# Include <unistd. h>
# Include <fcntl. h>
# Include "JNI. H"
# Include "jnihelp. H"
# Include "android_runtime/androidruntime. H"
//----------------------------------------------------------------------------
Using namespaceandroid;
Static voidandroid_pmps_pmpsmanager_sayhello (jnienv * ENV, jobject thiz)
{
Logv ("sayhello ()");
}
Static constjninativemethod method_table [] = {
{"Sayhello", "() V", (void *) android_pmps_pmpsmanager_sayhello },
};
// This function onlyregisters the native Methods
Intregister_android_pmps_pmpsmanager (jnienv * env ){
Inti;
Logi ("jni_onload: register_android_pmps_pmpsplayer ");
Returnandroidruntime: registernativemethods (ENV,
"Android/PMPS/pmpsmanager", method_table, nelem (method_table ));
}
Jintjni_onload (JavaVM * Vm, void * Reserved)
{
Jnienv * Env = NULL;
Jint result =-1;
If (Vm-> getenv (void **) & ENV, jni_version_1_4 )! = Jni_ OK ){
LogE ("error: getenvfailed \ n ");
Goto bail;
}
Assert (ENV! = NULL );
If (register_android_pmps_pmpsmanager (ENV) <0 ){
LogE ("error: pmpsmanager nativeregistration failed \ n ");
Goto bail;
}
/* Success -- Return valid version number */
Result = jni_version_1_4;
Bail:
Return result;
}