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     };

 

转载于:https://www.cnblogs.com/l2rf/p/5018372.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值