您的位置:首页 > 移动开发 > Android开发

自定义外部按键实现android对按键事件的响应实现流程

2012-12-06 17:07 441 查看
自定义外部按键(硬件),实现android对按键事件的响应实现流程:

1、内核层:

将按键驱动做好,并参考include/linux/input.h

#define KEY_F20 190
#define KEY_F21 191
#define KEY_F22 192
#define KEY_F23 193
#define KEY_F24 194

修改 arch/arm/mach-s3c64xx/mach-mini6410.c

static struct gpio_keys_button gpio_buttons[] = {
……………
{
.gpio = S3C64XX_GPL(12),
.code = 190,

.desc = " FUNCTION_F1",
.active_low = 1,
.wakeup = 0,
}, {
.gpio = S3C64XX_GPN(3),
.code = 191,
.desc = " FUNCTION_F2",
.active_low = 1,
.wakeup = 0,
}, {
.gpio = S3C64XX_GPN(4),
.code = 192,
.desc = " FUNCTION_F3",
.active_low = 1,
.wakeup = 0,
}, {
.gpio = S3C64XX_GPN(5),
.code = 193,
.desc = " FUNCTION_F4",
.active_low = 1,
.wakeup = 0,
}, {
.gpio = S3C64XX_GPN(2),
.code = 194,
.desc = " FUNCTION_F5",
.active_low = 1,
.wakeup = 0,
}
};

2、android文件系统层

1)、/frameworks/base/include/ui/keycodeLabels.h

在数组static const KeycodeLabel KEYCODES[]
中添加新定义的信息

{ "FUNCTION_F1", 111 },

{ "FUNCTION_F2", 112 },

{ "FUNCTION_F3", 113 },

{ "FUNCTION_F4", 114 },

{ "FUNCTION_F5", 115 },

2)、 ./ frameworks/base/native/include/android/Keycodes.h
枚举类型中添加

AKEYCODE_FUNCTION_F1 = 111,

AKEYCODE_FUNCTION_F2 = 112,

AKEYCODE_FUNCTION_F3 = 113,

AKEYCODE_FUNCTION_F4 = 114,

AKEYCODE_FUNCTION_F5 = 115,

3)、 ./frameworks/base/core/res/res/values/attrs.xml

<enum name="KEYCODE_FUNCTION_F1" value="111" />

<enum name="KEYCODE_FUNCTION_F2" value="112" />

<enum name="KEYCODE_FUNCTION_F3" value="113" />

<enum name="KEYCODE_FUNCTION_F4" value="114" />

<enum name="KEYCODE_FUNCTION_F5" value="115" />

4)、 ./frameworks/base/core/java/android/view/KeyEvent.java

public static final int KEYCODE_FUNCTION_F1 = 111;

public static final int KEYCODE_FUNCTION_F2 = 112;

public static final int KEYCODE_FUNCTION_F3 = 113;

public static final int KEYCODE_FUNCTION_F4 = 114;

public static final int KEYCODE_FUNCTION_F5 = 115;

并修改

private static final int LAST_KEYCODE = KEYCODE_FUNCTION_F5;

5)、 ./frameworks/base/libs/ui/Input.java

在bool KeyEvent::isSystemKey(int32_t keyCode)()中,同样需要添加:

case AKEYCODE_FUNCTION_F1:

case AKEYCODE_FUNCTION_F2:

case AKEYCODE_FUNCTION_F3:

case AKEYCODE_FUNCTION_F4:

case AKEYCODE_FUNCTION_F5:

6)、通过以上的更改,新的键值就添加上去了,注意上面标红色的数字表示必须相同的,另外由于更改了 KeyEvent,影响到了API,所以需要make
update-api

7)、修改system/usr/keylayout/的qwerty.kl文件

key 194 FUNCTION_F5 WAKE_DROPPED
key 193 FUNCTION_F4 WAKE_DROPPED
key 192 FUNCTION_F3 WAKE_DROPPED
key 190 FUNCTION_F1 WAKE_DROPPED
key 191 FUNCTION_F2 WAKE_DROPPED

7)、应用程序以两种方式响应:

a: 应用程序的View或者Activity通过调用onKeyDown()或onKeyUp()直接监听按键消息。

public boolean onKeyDown(int kCode,KeyEvent kEvent)

{

switch(kCode)

{

case KeyEvent. FUNCTION_F1:

Log.v("MyKeyDown","onkeydown= FUNCTION_F1");

break;

…………..
}

}

b:在系统中对新键值做特殊处理

如果对新键值进行处理,可以通过获取相应的keycode,对它进行处理;对于按键事件的处理一般如下文件中:./frameworks/base/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java

的函数

public boolean interceptKeyBeforeDispatching(WindowState win, int action, int flags,
int keyCode, int metaState, int repeatCount, int policyFlags) {
……………………………….
// added by joqian 20121026 start

if ((keyCode == KeyEvent.KEYCODE_FUNCTION_F1) && !down)

{
String actionString = "com.axent.succi.controller.keycode_function1";

Intent intent = new Intent(actionString);
mContext.sendBroadcast(intent);
Log.i(TAG, "interceptKeyBeforeDispatching function KEYCODE_FUNCTION_F1 by joqian\n");
}
if ((keyCode == KeyEvent.KEYCODE_FUNCTION_F2) && !down)

{
String actionString = "com.axent.succi.controller.keycode_function2";

Intent intent = new Intent(actionString);
mContext.sendBroadcast(intent);
Log.i(TAG, "interceptKeyBeforeDispatching function KEYCODE_FUNCTION_F2 by joqian\n");
}
if ((keyCode == KeyEvent.KEYCODE_FUNCTION_F3) && !down)

{
String actionString = "com.axent.succi.controller.keycode_function3";

Intent intent = new Intent(actionString);
mContext.sendBroadcast(intent);
Log.i(TAG, "interceptKeyBeforeDispatching function KEYCODE_FUNCTION_F3 by joqian\n");
}
if ((keyCode == KeyEvent.KEYCODE_FUNCTION_F4) && !down)

{
String actionString = "com.axent.succi.controller.keycode_function4";

Intent intent = new Intent(actionString);
mContext.sendBroadcast(intent);
Log.i(TAG, "interceptKeyBeforeDispatching function KEYCODE_FUNCTION_F4 by joqian\n");
}

// added by joqian 20121026 end
…………………………………
}

3、应用程序层:

1)、首先建立一个应用程序,并且建立一个类

package com.axent;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class myBroadCast extends BroadcastReceiver
{
public myBroadCast()
{
Log.v("myBroadCast","myBroadCast");

}

@Override
public void onReceive(Context context, Intent intent)
{
Log.v("myBroadCast", "onReceive");

}
}

2)、并且修改AndroidManifest.xml,添加红色代码部分:

<application android:label="@string/app_name"
android:icon="@drawable/ic_launcher"
android:theme="@style/AppTheme">

<receiver android:name=".myBroadCast">
<intent-filter>

<action android:name="com.axent.succi.controller.keycode_function1"/>

<action android:name="com.axent.succi.controller.keycode_function2"/>

<action android:name="com.axent.succi.controller.keycode_function3"/>

<action android:name="com.axent.succi.controller.keycode_function4"/>

</intent-filter>

</receiver>

</application>

参考:
Linux驱动和android文件系统的修改方法参考:/article/8906526.html
/article/8906526.html

应用程序的编写方法参考:http://www.cnblogs.com/TerryBlog/archive/2010/08/16/1801016.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: