android mod 框架,【转】Android开发:Framework添加新模块

为应用添加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

**

**

** 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

#include

#include

#include

#include

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

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值