java jni 键盘_好记性不如烂笔头------按键灯jni使用(上层)

上层使用jni(从java层到framework到JNI的用法过程)

MTK平台如果在硬件上增加了按键灯,framework层想要调用的话,这里简单介绍一下我的使用过程。

首先,需要增加KeyLed.c和头文件KeyLed.h两个接口函数:

(\hardware\libhardware_legacy\key_leds\key_leds.c)

( \hardware\libhardware_legacy\include\hardware_legacy\KeyLed.h)

接下来需要一步步向上写接口了:

1,给灯增加权限:

进入到system/core/rootdir目录,里面有一个名为ueventd.rc文件,往里面添加一行:

/sys/devices/platform/key_leds/red1_leds_onoff 0666 root root

/sys/devices/platform/key_leds/red2_leds_onoff 0666 root root

/sys/devices/platform/key_leds/red3_leds_onoff 0666 root root

2,进入到frameworks/base/services/jni目录,新建com_android_server_KeyLedsService.cpp文件:

在com_android_server_KeyLedsService.cpp文件中,实现JNI方法。注意文件的命令方法,com_android_server前缀表示的是包名,表示硬件服务HelloService是放在frameworks/base/services/java目录下的com/android/server目录的,即存在一个命令为com.android.server.KeyLedsService的类。简单地说,KeyLedsService是一个提供Java接口的硬件访问服务类。

//首先是包含相应的头文件:

//在硬件抽象层中定义的硬件访问结构体,

//JNI方法表

//注册JNI方法

3,修改同目录下的onload.cpp文件,首先在namespace android增加register_android_server_KeyLedsService函数声明:

namespace android {

..............................................................................................

int register_android_server_KeyLedsService(JNIEnv *env);

};

在JNI_onLoad增加register_android_server_KeyLedsService函数调用:

extern "C" jint JNI_onLoad(JavaVM* vm, void* reserved)

{

.................................................................................................

register_android_server_KeyLedsService(env);

.................................................................................................

}

这样,在Android系统初始化时,就会自动加载该JNI方法调用表。

4, 修改同目录下的Android.mk文件,在LOCAL_SRC_FILES变量中增加一行:

LOCAL_SRC_FILES:= \

.............................................................

com_android_server_KeyLedsService.cpp \

onload.cpp

此时编译代码,重新打包的system.img镜像文件就包含我们刚才编写的JNI方法了,也就是我们可以通过Android系统的Application Frameworks层提供的硬件服务KeyLedsService来调用这些JNI方法,进而调用低层的硬件抽象层接口去访问硬件了。

数字科技日新月异的今天,软件和硬件的完美结合,造就了智能移动设备的流行。今天大家对iOS和Android系统的趋之若鹜,一定程度上是由于这两个系统上有着丰富多彩的各种应用软件。因此,软件和硬件的关系,在一定程度上可以说,硬件是为软件服务的。硬件工程师研发出一款硬件设备,自然少不了软件工程师为其编写驱动程序;而驱动程序的最终目的,是为了使得最上层的应用程序能够使用这些硬件提供的服务来为用户提供软件功能。对Android系统上的应用软件来说,就是要在系统的Application Frameworks层为其提供硬件服务。Linux内核层、硬件抽象层和运行时库层提供的自定义硬件服务接口,这些接口都是通过C或者C++语言来实现的。接下来,我们将学习如何在Android系统的Application Frameworks层提供Java接口的硬件服务。

4.2,4.4,5.1等不同版本文件路径稍有不同,这里使用的是4.4版本

1,增加IKeyLedsManager.aidl,(framework/base/core/java/android/app/IKeyLedsManager.aidl)

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 package android.app;2

3 interfaceIKeyLedsManager4 {5 voidred1_leds_exists();6 voidred2_leds_exists();7 voidred3_leds_exists();8 voidblue_leds_exists();9 voidgreen_leds_exists();10 voidred1_leds_on();11 voidred1_leds_off();12 voidred2_leds_on();13 voidred2_leds_off();14 voidred3_leds_on();15 voidred3_leds_off();16 voidblue_leds_on();17 voidblue_leds_off();18 voidgreen_leds_on();19 voidgreen_leds_off();20 }

View Code

主要定义了KeyLedsService的接口,IKeyLedsManager接口主要提供了设备和获取硬件寄存器val的值的功能,通过几个具体函数来实现。

2,返回到frameworks/base目录,打开Android.mk文件,修改LOCAL_SRC_FILES变量的值,增加IKeyLedsManager.aidl源文件:

LOCAL_SRC_FILES += /

....................................................................

core/java/android/os/IVibratorService.aidl /

core/java/android/os/IKeyLedsManager.aidl /

core/java/android/service/urlrenderer/IUrlRendererService.aidl /

.....................................................................

此时编译,就会根据IKeyLedsManager.aidl生成相应的IKeyLedsManager.Stub接口。

3,增加KeyLedsManager.java,KeyLedsManager.java主要是通过调用JNI方法来提供硬件服务。

(framework/base/core/java/android/app/KeyLedsManager.java)

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 /*

2 * Copyright (C) 2008 The Android Open Source Project3 *4 * Licensed under the Apache License, Version 2.0 (the "License");5 * you may not use this file except in compliance with the License.6 * You may obtain a copy of the License at7 *8 *http://www.apache.org/licenses/LICENSE-2.0

9 *10 * Unless required by applicable law or agreed to in writing, software11 * distributed under the License is distributed on an "AS IS" BASIS,12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13 * See the License for the specific language governing permissions and14 * limitations under the License.15 */

16

17 packageandroid.app;18

19 importandroid.content.Intent;20 importandroid.os.Handler;21 importandroid.os.HandlerThread;22 importandroid.os.IBinder;23 importandroid.os.Looper;24 importandroid.os.Message;25 importandroid.util.Log;26 importandroid.os.RemoteException;27 importandroid.content.Context;28

29 public classKeyLedsManager {30 public static final String TAG = "KeyLedsManager";31 IKeyLedsManager mService;32 Context mContext;33 Handler mHandler;34

35 private final Runnable mOff = newRunnable() {36 public voidrun() {37 Log.d("__ngy-->", "mOff run , turn breath leds off");38 keyLedsAllOff();39 }40 };41

42 publicKeyLedsManager(Context ctx, IKeyLedsManager service) {43 mContext =ctx;44 mService =service;45 Log.d(TAG, "mContext=" +mContext);46 }47

48 public voidredKeyLedsTurnOn() {49 Log.d(TAG, "keyLedsTurnOn");50 red1_leds_on();51 red2_leds_on();52 red3_leds_on();53 }54

55 public voidgreenkeyLedsTurnOn() {56 Log.d(TAG, "keyLedsTurnOn");57 green_leds_on();58 }59

60 public voidkeyLedsAllOff(){61 Log.d(TAG, "keyLedsAllOff");62 Log.d(TAG, "turn key leds off");63 mHandler.removeCallbacks(mOff);64

65 red1_leds_off();66 red2_leds_off();67 red3_leds_off();68 blue_leds_off();69 green_leds_off();70 }71

72 public voidred1_leds_exists(){73 try{74 mService.red1_leds_exists();75 } catch(NullPointerException e){76 Log.e(TAG, "NullPointerException=" +e);77 } catch(RemoteException e) {78 Log.e(TAG, "[red1_leds_exists] RemoteException");79 }80 }81

82 public voidred2_leds_exists(){83 try{84 mService.red2_leds_exists();85 } catch(NullPointerException e){86 Log.e(TAG, "NullPointerException=" +e);87 } catch(RemoteException e) {88 Log.e(TAG, "[red2_leds_exists] RemoteException");89 }90 }91

92 。。。。。。。。。。。。。。。。。。。。。。。93 。。。。。。。。。。。。。。。。。。。。。。。。。。。94

95 public voidgreen_leds_off(){96 try{97 mService.green_leds_off();98 } catch(NullPointerException e){99 Log.e(TAG, "NullPointerException=" +e);100 } catch(RemoteException e) {101 Log.e(TAG, "[green_leds_off] RemoteException");102 }103 }104 }

View Code

Current.txt(framework/base/api/current.txt),这个修改后更新需要 make update-api

ContextImpl.java(framework/base/core/java/android/app/ContextImpl.java)(编译后自动更新)

4,进入到frameworks/base/services/java/com/android/server/目录,新增keyleds/KeyLedsService.java文件:

KeyLedsService主要是通过调用JNI方法来控制不同状态(未读短信,未接电话等)下的灯的状态

5,修改SystemServer.java文件,声明这个service,在初始化函数中增加加载KeyLedsService的代码:

(frameworks\base\services\java\com\android\server\SystemServer.java)

KeyLedsService mKeyLedsService = null;

mKeyLedsService = new KeyLedsService(context);

ServiceManager.addService(Context.KEYLEDS_SERVICE, mKeyLedsService);

在上层使用的时候,声明KeyLedsManager,调用方法即可;

参考文档:http://blog.csdn.net/poltroon/article/details/7978264

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值