如何从标准输入 (stdin) 捕获进入我的 Android 应用程序的数据? [英] How can I capture data coming into my Android app from the standard input (stdin)?

查看:40
本文介绍了如何从标准输入 (stdin) 捕获进入我的 Android 应用程序的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个使用外部 USB 条形码/RFID 扫描仪的应用程序.扫描仪是标准的 HID,在我的 Android 设备上运行良好.我插入它,点击扫描按钮,数据就会在文本编辑应用程序中弹出.标准 USB 键盘也是如此.我插上它,开始输入,然后数据就会显示在文本编辑应用中.

I am writing an app that uses an external USB barcode/RFID scanner. The scanner is a standard HID, and works well on my Android devices. I plug it in, hit the scan button, and data pops up in a text editing app. The same can be said for a standard USB keyboard. I plug it in, start typing, and data shows up in a text editing app.

这里是我需要帮助的地方.我需要做的是操作从扫描仪或外部键盘进入我的应用的数据,然后才能将其放入应用的正确表单字段中.

Here is where I need assistance. What I need to do is manipulate the data coming into my app, from the scanner or external keyboard, before I can place it into the correct form fields of the app.

此时,我想我可以截取标准输入处的数据,所以我想出了这个:

At this point, I thought I could just intercept the data at the standard input, so this is what I came up with:

activity_main.xml

<LinearLayout>
    <EditText android:id="@+id/scan_data" />
</LinearLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    EditText scannedInput = (EditText)findViewById(R.id.scan_data);

    BufferedReader scanner = new BufferedReader(new InputStreamReader(System.in));

    StringBuilder sBuilder = new StringBuilder();
    String buffer;

    while((buffer = scanner.readLine()) != null){
        sBuilder.append(buffer);
    }

    String[] dataPoints = sBuilder.toString().split("\u003D|\u0026");

    scannedInput.setTextColor(Color.RED);
    scannedInput.setTextSize(34f);
    scannedInput.setText(dataPoints[0]); // or dataPoints[1], dataPoints[2], etc.
}

我写了这篇文章,连接了我的扫描仪,扫描了一个 RFID 标签.令我惊讶的是,整个扫描的字符串出现在我的 EditText 字段中,而不仅仅是一部分.经过一些调试,我发现我可以删除 setContentView() 之后的所有代码,并且整个字符串仍然显示在我的 EditText 字段中.

I wrote this, hooked up my scanner, and scanned an RFID tag. To my surprise, the entire scanned string showed up in my EditText field, and not just a part. After some debugging, I found out I could delete all code after setContentView() and the entire string still shows up in my EditText field.

所以,我需要知道的是,如何从任何外部来源(例如扫描仪、键盘等)捕获标准输入,并在将其放置在我想要的位置(表单字段、数据库等)之前对其进行操作.).

So, what I need to know is, how I can capture the stdin from any external source (e.g., scanner, keyboard, etc.), and manipulate it before placing it where I want it (form field, database, etc.).

我该怎么做?

推荐答案

简而言之,您无法访问 stdin,Android 中没有这样的应用程序.文本被注入到活动 EditText 中,因为您使用的 RFID/USB 设备将自身显示为 HID 设备.Android 子系统会自动选取这些作为输入源,并将它们的输入路由到活动视图,就像来自键盘一样.

The short answer is that you can't access stdin, there is no such animal in Android for apps. The text is being injected into the active EditText because the RFID / USB device you are using is presenting itself as a HID device. These are automatically picked up by the Android subsystem as an input source and their input routed to the active view as if it came from a keyboard.

然而,一切都没有丢失.您可以做的是将 TextWatcher 附加到您的 EditText 并操作 Editable 中的文本:

All is not lost, however. What you can do is attach a TextWatcher to your EditText and manipulate the text in the Editable:

EditText et = (EditText)findViewById(R.id.whatever_your_id);
et.addTextChangedListener(new TextWatcher() {
        @Override
        void afterTextChanged (Editable s) {
            //  Make your changes to 's' here, carefully as your changes will
            //  cause this method to be called again!
        }

        @Override
        void beforeTextChanged(CharSequence s, int start, int count, int after) {
            //  Nothing to do here
        }

        @Override
        void onTextChanged(CharSequence s, int start, int before, int count) {
            //  Nothing to do here
        }
    });

这篇关于如何从标准输入 (stdin) 捕获进入我的 Android 应用程序的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆