Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android給自定義按鍵添加廣播和通過廣播給當前焦點輸入框賦值

Android給自定義按鍵添加廣播和通過廣播給當前焦點輸入框賦值

編輯:關於Android編程

一、給自定義按鍵添加廣播

修改PhoneWindowManager.java中的interceptKeyBeforeDispatching方法

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

@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

感謝閱讀,希望能幫助到大家,謝謝大家對本站的 支持!

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved