為應用添加framework介面,即SDK API
在framework/base目錄建立pmps目錄,在pmps目錄建立\java\android\pmps\PmpsManager.java 檔案,輸入代碼:
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 為應用程式層提供相關API,如 sayHello(),這裡是直接調用了JNI的native方法。值得注意的是PmpsManager載入了中的System.loadLibrary("pmps_jni")載入了文章中第3步將會產生的libpmps_jni.so檔案。
還有一個需要注意的細節,我們可以先看一下framework/base目錄下的Android.mk檔案,其中有一段:
# FRAMEWORKS_BASE_SUBDIRS comes from build/core/pathmap.mk
LOCAL_SRC_FILES := $(call find-other-java-files,$(FRAMEWORKS_BASE_SUBDIRS))
我們再看看build/core/pathmap.mk,其中有一段
FRAMEWORKS_BASE_SUBDIRS := \
$(addsuffix /java, \
core \
graphics \
location \
media \
opengl \
sax \
telephony \
wifi \
vpn \
keystore \
voip \
)
也就是說我們要在build/core/pathmap.mk檔案中添加pmps目錄,添加後為:
FRAMEWORKS_BASE_SUBDIRS := \
$(addsuffix /java, \
core \
graphics \
location \
media \
opengl \
sax \
telephony \
wifi \
vpn \
keystore \
voip \
pmps \
)
然後我們就可以通過 mmm 命令編譯這個新添加的模組了。
mmm framework/base 編譯,組建檔案為out/target/product/generic/system/framework/framework.jar
代碼編譯通過後,就可以做接下來的事情了。
2.framework調用native代碼,即jni部分
上一步中的 publicnative static void sayHello() 就可以由應用程式層調用了,當然我們可以在framework裡面再封裝一下,以實現更加複雜的功能,例如:
//api
public int sayHelloFirst()
{
//省略的代碼...
return sayHello();
}
//declaration
public native static void sayHello();
3.jni部分實現功能
這一步通過編寫native代碼實現framework中的調用的功能,可以實現具體的功能或者調用hal層的相關驅動,完成硬體操作功能。
當前只是簡單地列印一下資訊。
值得注意的要實現JNI_OnLoad函數,如果有需要的話,也可以重寫JNI_OnUnLoad函數。
/*//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 except in compliance with the License.
** You may obtain acopy of the License at
**
** 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;
}