欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android给自定义按键添加广播和通过广播给当前焦点输入框赋值

程序员文章站 2024-03-02 20:15:10
一、给自定义按键添加广播 修改phonewindowmanager.java中的interceptkeybeforedispatching方法 /frameworks/...

一、给自定义按键添加广播

修改phonewindowmanager.java中的interceptkeybeforedispatching方法

/frameworks/base/policy/src/com/android/internal/policy/impl/phonewindowmanager.java

Android给自定义按键添加广播和通过广播给当前焦点输入框赋值

@override
  public long interceptkeybeforedispatching(windowstate win, keyevent event, int policyflags) {
    final boolean keyguardon = keyguardon();
    final int keycode = event.getkeycode();
    final int repeatcount = event.getrepeatcount();
    final int metastate = event.getmetastate();
    final int flags = event.getflags();
    final boolean down = event.getaction() == keyevent.action_down;
    final boolean canceled = event.iscanceled();
    final boolean longpress = (flags & keyevent.flag_long_press) != 0;
    final boolean virtualkey = event.getdeviceid() == keycharactermap.virtual_keyboard;
    final string iscitkeytest=systemproperties.get("persist.sys.startcitkeytest");


    
    //添加自定义按键广播----开始
    switch (keycode)
      {
        case 135:
        case 136:
        case 137:
        case 139:
        {
          if (repeatcount == 0) {
            
            intent mykeyintent = new intent();
            
            
            if(down)
            {
              mykeyintent.setaction("com.rscja.android.key_down");
            }
            else
            {
              mykeyintent.setaction("com.rscja.android.key_up");
            }
            
          
            mykeyintent.putextra("keycode", keycode);
            
            mcontext.sendbroadcastasuser(mykeyintent, userhandle.all); 
            
            
          
          }
        }
        
        
        
          break;
      }
      
      //添加自定义按键广播----结束




……
……
……
……

}

二、通过广播给当前焦点输入框赋值

修改inputmethodservice.java中的oncreate()方法

/frameworks/base/core/java/android/inputmethodservice/inputmethodservice.java

Android给自定义按键添加广播和通过广播给当前焦点输入框赋值

Android给自定义按键添加广播和通过广播给当前焦点输入框赋值

感谢阅读,希望能帮助到大家,谢谢大家对本站的 支持!