Android给自定义按键添加广播和通过广播给当前焦点输入框赋值
生活随笔
收集整理的這篇文章主要介紹了
Android给自定义按键添加广播和通过广播给当前焦点输入框赋值
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、給自定義按鍵添加廣播
修改PhoneWindowManager.java中的interceptKeyBeforeDispatching方法
?
/frameworks/base/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java
?
1 @Override2 public long interceptKeyBeforeDispatching(WindowState win, KeyEvent event, int policyFlags) {3 final boolean keyguardOn = keyguardOn();4 final int keyCode = event.getKeyCode();5 final int repeatCount = event.getRepeatCount();6 final int metaState = event.getMetaState();7 final int flags = event.getFlags();8 final boolean down = event.getAction() == KeyEvent.ACTION_DOWN;9 final boolean canceled = event.isCanceled(); 10 final boolean longPress = (flags & KeyEvent.FLAG_LONG_PRESS) != 0; 11 final boolean virtualKey = event.getDeviceId() == KeyCharacterMap.VIRTUAL_KEYBOARD; 12 final String isCitKeyTest=SystemProperties.get("persist.sys.startCitKeyTest"); 13 14 15 16 //添加自定義按鍵廣播----開始 17 switch (keyCode) 18 { 19 case 135: 20 case 136: 21 case 137: 22 case 139: 23 { 24 if (repeatCount == 0) { 25 26 Intent myKeyIntent = new Intent(); 27 28 29 if(down) 30 { 31 myKeyIntent.setAction("com.rscja.android.KEY_DOWN"); 32 } 33 else 34 { 35 myKeyIntent.setAction("com.rscja.android.KEY_UP"); 36 } 37 38 39 myKeyIntent.putExtra("keycode", keyCode); 40 41 mContext.sendBroadcastAsUser(myKeyIntent, UserHandle.ALL); 42 43 44 45 } 46 } 47 48 49 50 break; 51 } 52 53 //添加自定義按鍵廣播----結束 54 55 56 57 58 …… 59 …… 60 …… 61 …… 62 63 }?
二、通過廣播給當前焦點輸入框賦值
?
修改InputMethodService.java中的onCreate()方法
/frameworks/base/core/java/android/inputmethodservice/InputMethodService.java
?
?
1 @Override public void onCreate() {2 mTheme = Resources.selectSystemTheme(mTheme,3 getApplicationInfo().targetSdkVersion,4 android.R.style.Theme_InputMethod,5 android.R.style.Theme_Holo_InputMethod,6 android.R.style.Theme_DeviceDefault_InputMethod,7 android.R.style.Theme_DeviceDefault_InputMethod);8 super.setTheme(mTheme);9 super.onCreate(); 10 mImm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE); 11 mInflater = (LayoutInflater)getSystemService( 12 Context.LAYOUT_INFLATER_SERVICE); 13 mWindow = new SoftInputWindow(this, "InputMethod", mTheme, null, null, mDispatcherState, 14 WindowManager.LayoutParams.TYPE_INPUT_METHOD, Gravity.BOTTOM, false); 15 if (mHardwareAccelerated) { 16 mWindow.getWindow().addFlags(WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED); 17 } 18 initViews(); 19 mWindow.getWindow().setLayout(MATCH_PARENT, WRAP_CONTENT); 20 21 22 //注冊廣播監聽 23 IntentFilter barCodeCheckFilter = new IntentFilter("com.rscja.android.DATA_RESULT"); 24 mRootView.getContext().registerReceiver(m_barCodeCheckReciever,barCodeCheckFilter); 25 26 } 27 28 29 //添加自定義廣播 30 private BroadcastReceiver m_barCodeCheckReciever = new BroadcastReceiver () { 31 32 @Override 33 public void onReceive(Context context, Intent intent) { 34 InputConnection conn = getCurrentInputConnection(); 35 36 String intent_data=intent.getStringExtra("data"); 37 38 39 if(intent_data.charAt(intent_data.length() - 1)=='\n') 40 { 41 if (conn != null) { 42 conn.commitText(intent_data.substring(0,intent_data.length()-1), 1); 43 } 44 sendKeyChar('\n'); 45 } 46 else 47 { 48 if (conn != null) { 49 conn.commitText(intent_data, 1); 50 } 51 } 52 } 53 };?
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的Android给自定义按键添加广播和通过广播给当前焦点输入框赋值的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: AndroidStudio直接通过gra
- 下一篇: ubuntu16.04 server u