Background:

Unified app is used to monitor the enter originally to realize the PDA scanning gun scan, the results one day suddenly bugs, scanning head always automatically shut down the keyboard of setting up the output (bi) for dogs, and then with this code, use the radio to realize scanning and press the keyboard to enter, or by listening to enter after implementation, in order to compatible (lazy). Of course, the code also provides other ways, specific can modify ha. Make it and run. It’s so exciting.

Steps:

1. View PDA scan to set broadcast parameters

2. Define the broadcast receiver

package com.hxb.common.util; import android.app.Instrumentation; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; import android.view.KeyEvent; import android.widget.EditText; import com.hxb.base.BaseApplication; / barcode notice * * * * radio after receive analog output enter * * @ author h6900 * / public class ScanCodeBroadcastReceiver extends BroadcastReceiver {/ * * * */ private EditText[] editTexts; /** * private OnReceiveCode onReceive; Private String TAG = ""; @Override public void onReceive(Context context, Intent intent) { String code = intent.getStringExtra("barcode_string");  Log.i(TAG, "--" + BaseApplication.isTopActivity(TAG)); if (BaseApplication.isTopActivity(TAG)) { int id = outputEditTextByFocus(code); onReceive.notifyThread(code, id); }} /** * outputEditTextByFocus(String) * @param Code The code returned by the broadcast * @return The current focus input box ID */ private int outputEditTextByFocus(String) code){ for (EditText editText : editTexts) { Log.i(TAG, editText.hasFocus() + "--" + editText.getId() + ""); if (editText.hasFocus()) { Log.i(TAG, editText.getId() + ""); editText.setText(code); New Thread(new Runnable() {@override public void run() {Instrumentation inst = new Instrumentation(); inst.sendKeyDownUpSync(KeyEvent.KEYCODE_ENTER); } }).start(); return editText.getId(); } } return 0; } public interface OnReceiveCode {/** * notification ** @param code * @param ID */ void notifyThread(String code, int ID); } public void setOnReceive(String tag, OnReceiveCode onReceive, EditText[] editTexts) { this.TAG = tag; this.onReceive = onReceive; this.editTexts = editTexts; }}Copy the code

3. Define the top-level Activity of the current app in the Application

/** * @author h6900 */ public class BaseApplication extends Application implements Application.ActivityLifecycleCallbacks { private static String topActivity; @Override public void onCreate() { super.onCreate(); registerActivityLifecycleCallbacks(this); initSDK(this); } /** ** initialize some third-party frameworks * @param baseApplication */ private void initSDK(baseApplication baseApplication) {//ARouter ARouter.openLog(); // Prints the log arouter.openDebug (); // initialize ARouter ARouter. Init (this); } public static Boolean isTopActivity(String tag) {if (String tag) {if (String tag) {if (String tag) {if (String tag)} (TextUtils.isEmpty(topActivity)) { return false; } return topActivity.contains(tag); } @Override public void onActivityCreated(Activity activity, Bundle savedInstanceState) { } @Override public void onActivityStarted(Activity activity) { } @Override public void onActivityResumed(Activity activity) { Log.i("APP",activity.getClass().getName()); topActivity = activity.getClass().getName(); } @Override public void onActivityPaused(Activity activity) { } @Override public void onActivityStopped(Activity activity) { } @Override public void onActivitySaveInstanceState(Activity activity, Bundle outState) { } @Override public void onActivityDestroyed(Activity activity) { } }Copy the code

4. Define the init broadcast method in baseActivity

private ScanCodeBroadcastReceiver setOnReceive; protected final String TAG = this.getClass().getName(); protected void initBarcodeBroadcastReceiver(ScanCodeBroadcastReceiver.OnReceiveCode onReceiveCode, EditText[] editTexts) { if (Declare.pdaBroadcast ! = null && ! TextUtils.isEmpty(Declare.pdaBroadcast.getAction()) && ! TextUtils.isEmpty(Declare.pdaBroadcast.getStringExtra())) { setOnReceive = new ScanCodeBroadcastReceiver(); IntentFilter intentFilter = new IntentFilter(); //"android.intent.ACTION_DECODE_DATA" intentFilter.addAction(Declare.pdaBroadcast.getAction()); registerReceiver(setOnReceive, intentFilter); setOnReceive.setOnReceive(TAG,onReceiveCode, editTexts); }}Copy the code

5. Destroy the broadcast receiver in onDestroy

@Override protected void onDestroy() { super.onDestroy(); // Destroy if (setOnReceive! = null) { unregisterReceiver(setOnReceive); }}Copy the code

6. Use it on the page

InitBarcodeBroadcastReceiver (Code, id) - > {/ / out Code in id}, new EditText [] {workOrder});Copy the code

7. You can also use the carriage return event. If you do not need to modify the class in Step 2, you can use custom listener or carriage return listener

workOrder.setOnKeyListener(new View.OnKeyListener() { @Override public boolean onKey(View v, int keyCode, KeyEvent event) {if (keyCode == keyevent.keycode_enter && event.getAction() == keyevent.action_down) {// Listen for the return key //do some } return false; }});Copy the code

The final releaseThe source code