Android下添加自定义按键

2017-09-13 20:38:58来源:CSDN作者:u012966861人点击

分享

 

1)frameworks/base/data/keyboards/Generic.kl文件中添加映射

key 185  F15

 

key -- 是关键字。固定值,不需要改变。

185 -- 该按键在linux驱动中对应的键值,我在这里只是假设的这个值,至于具体对应的是那个可以查看“include/linux/input.h
F15  -- 185按键映射到
Android中的按键,它对应是“InputEventLabels.h”文件中的KEYCODES表格元素的“literal”值。

 

2)framework/native/include/input/InputEventLabels.h添加

DEFINE_KEYCODE(F15),//F15 AKEYCODE_F15

 

3)framework/native/include/android/keycodes.h

AKEYCODE_F15=282

 

4)framework/base/core/res/res/values/attrs.xml

<enum name=”KEYCODE_F15” value=”282”/>

 

5)framework/base/core/java/android/view/KeyEvent.java

public static final int KEYCODE_F15=282;

 

经过如上的步骤就将"185"按键和Android系统中的KEYCODE_F15就对应起来了

 

6)Androidframework层的键值处理函数中,捕获按键,并进行相应处理。对应的文件是frameworks/base/services/core/java/com/android/server

/policy/PhoneWindowManager.java
添加的具体方法:在PhoneWindowManager.java的interceptKeyBeforeQueueing()函数中,捕获相应的键值,进行相关处理。

 

7frameworks/base/api/current.txt添加

field public static final int KEYCODE_F15=282;//0x11a

 

8)framework/base/api/system-current.txt添加

field public static final int KEYCODE_F15=282;//0x11a

 

9)framework/base/api/test-current.txt添加

field public static final int KEYCODE_F15=282;//0x11a

 

以添加长按指纹拍照为例

package/apps/Settings/res/values/string.xml添加

<string name=”security_settings_fingerprint_take_photo_title”>TakePhoto</string>

<string name=”security_settings_fingerprint_take_photo_summary”>Fromthe viewfinder screen long press the fingerprint sensor to take aphoto.</string>


packages/apps/Settings/src/com/android/settings/fingerprint/FingerprintSettings.java添加

public static final String KEY_FINGERPRINT_TAKEPHOTO_SETTINGS=”fingerprint_take_photo_settings”;

public static SwitchPreferencegetFingerprintTakePhotoSwiechPreferenceForUser(Context context,fina intuserId){

      FingerprintManagerfpm=(FingerprintManager)context.getSystemService(Context.FINGERPRINT_SERVICE);

      if(fpm==null ||!fpm.isHardwareDetected()){

           Log.v(TAG,”Nofingerprint hardware detected!”);

           return 0;

}

SwitchPreferencefingerpintTakePhotoSwitchPreference=new SwitchPreference(context);

fingerprintTakePhotoSwitchPreference.setKey(KEY_FINGERPRINT_TAKEPHOTO_SETTINGS);

fingerprintTakePhotoSwitchPreferenec.setTitle(R.string.security_settings_fingerprint_take_photo_title);

fingerprintTakePhotoSwitchPreference.setSummary(

R.string.security_settings_fingerprint_take_photo_summary);

fingerprintTakePhotoSwitchPreference.setChecked(Settings.System.getInt(context.getContentResolver(),

      “fp_take_photo”,1)==1);

fingerprintTakePhotoSwitchPreference.setOnPreferenceClickListener(newOnPreferenceClickListener(){

      @Override

      public booleanonPreferenceClick(Preference preference){

           if(preference== fingerprintTakePhotoSwitchPreference){

                 boolean newState= fingerprintTakePhotoSwitchPreference.isChecked();

                 if(newState){

                      Setting.System.putInt(context.getContentResolver(),”fp_take_photo”,1);

                      fingerprintTakePhotoSweichPreference.setChecked(true);

                 }else{

                      Setting.System.putInt(context,getContentResolver(),”fp_take_photo”,0);

                      fingerprintTakePhotoSwitchPreference.setChecked(false);

                 }

           }

           return true;

      }

});

return fingerprintTakePhotoSwitchPreference;

}

 

packages/apps/Settings/src/com/android/settings/SecuritySettings.java中添加

方法maybeAddFingerprintPreference中添加

SwitchPreference fingerprintTakePhotoSwitchPreference=

      FingerprintSettings.getFingerprintTakePhotoSwitchPreferenceForUser(

           securityCategory.getContext(),userId);

 

if(fingerprintTakePhotoSwitchPreference!=null){

      securityCategory.addPreference(fingerprintTakePhotoSwitchPreference);

}

 

vendor/mediate/proprietary/package/apps/Camera/src/com/android/camera/actor/PhotoActor.java添加

import android.provider.Settings;

在方法onKeyUp中添加

case KeyEvent.KEYCODE_F15:

if(event.getRepeatCount()==0 && mIsInitalized

      &&Settings.System.getInt(mCameraActivity.getContentResolver(),”fp_take_photo”,1)==1){

      onShutterButtonClick(null);

      return true;

      }

      return false;

 

参考博客链接http://blog.csdn.net/tkwxty/article/details/43338921

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台