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

 

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

修改PhoneWindowManager.java中的interceptKeyBeforeDispatching方法

 

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

 

 1     @Override
 2     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     };

 

posted on 2015-12-04 09:33  屌丝迷途  阅读(2584)  评论(0编辑  收藏  举报

导航