Java字符串比较不可靠

时间:2017-06-26 14:15:13

标签: java android string comparison string-comparison

此问题暂时存在,并且不确定原因。

我正在从设备发送和接收数据。我想要做的就是将收到的消息与正在发送的对象上的变量集进行比较。

例如,我可以发送一个receiveValue为'Received 86 C0'的对象。

如果成功发送,我应该返回类似的内容:

IRDA1: Received 86 C0

我在日志名称'发送'下的对象上记录了返回值,然后我记录了名称'test'下返回的内容。

以下代码显示了我如何比较2个字符串:

public boolean checkReceived(DataCommand command) {
    if (TextUtils.isEmpty(message)) {
        return false;
    }
    if (message.contains(command.getCommandReturn())) {
        return true;
    }
    else {
        return false;
    }
}

现在它似乎不起作用。它最终确实有效,但需要永远。例如,下面的日志显示程序最终正确比较字符串所花费的次数。

06-26 15:04:33.820 24733-25852/com.smartscan.app.smartscanapp I/send: Received 8B FF
06-26 15:04:34.001 24733-24733/com.smartscan.app.smartscanapp I/test: IRDA1: Received 8B FF
06-26 15:04:35.838 24733-25852/com.smartscan.app.smartscanapp I/send: Received 8B FF
06-26 15:04:36.007 24733-24733/com.smartscan.app.smartscanapp I/test: IRDA1: Received 8B FF
06-26 15:04:37.856 24733-25852/com.smartscan.app.smartscanapp I/send: Received 8B FF
06-26 15:04:39.874 24733-25852/com.smartscan.app.smartscanapp I/send: Received 8B FF
06-26 15:04:40.035 24733-24733/com.smartscan.app.smartscanapp I/test: 1: Received 
06-26 15:04:41.891 24733-25852/com.smartscan.app.smartscanapp I/send: Received 8B FF
06-26 15:04:42.054 24733-24733/com.smartscan.app.smartscanapp I/test: IRDA1: Received 8B FF
06-26 15:04:43.971 24733-25852/com.smartscan.app.smartscanapp I/send: Received 8B FF
06-26 15:04:44.133 24733-24733/com.smartscan.app.smartscanapp I/test: IRDA1: Received 8B FF
06-26 15:04:45.991 24733-25852/com.smartscan.app.smartscanapp I/send: Received 8B FF
06-26 15:04:46.221 24733-24733/com.smartscan.app.smartscanapp I/test: IRDA1: Received 8B FF
06-26 15:04:48.009 24733-25852/com.smartscan.app.smartscanapp I/send: Received 8B FF
06-26 15:04:50.024 24733-25852/com.smartscan.app.smartscanapp I/send: Received 8B FF
06-26 15:04:50.191 24733-24733/com.smartscan.app.smartscanapp I/test: IRDA1: Received 8B FF
06-26 15:04:52.041 24733-25852/com.smartscan.app.smartscanapp I/send: Received 8B FF
06-26 15:04:54.060 24733-25852/com.smartscan.app.smartscanapp I/send: Received 8B FF
06-26 15:04:54.221 24733-24733/com.smartscan.app.smartscanapp I/test: IRDA1: Received 8B FF
06-26 15:04:56.078 24733-25852/com.smartscan.app.smartscanapp I/send: Received 8B FF
06-26 15:04:56.239 24733-24733/com.smartscan.app.smartscanapp I/test: IRDA1: Received 8B FF
06-26 15:04:58.097 24733-25852/com.smartscan.app.smartscanapp I/send: Received 8B FF
06-26 15:04:58.271 24733-24733/com.smartscan.app.smartscanapp I/test: IRDA1: Received 8B FF
06-26 15:05:00.112 24733-25852/com.smartscan.app.smartscanapp I/send: Received 8B FF
06-26 15:05:02.131 24733-25852/com.smartscan.app.smartscanapp I/send: Received 8B FF
06-26 15:05:02.305 24733-24733/com.smartscan.app.smartscanapp I/test: IRDA1: Received 8B FF
06-26 15:05:04.147 24733-25852/com.smartscan.app.smartscanapp I/send: Received 8B FF
06-26 15:05:06.165 24733-25852/com.smartscan.app.smartscanapp I/send: Received 8B FF
06-26 15:05:06.326 24733-24733/com.smartscan.app.smartscanapp I/test: IRDA1: Received 8B FF
06-26 15:05:08.183 24733-25852/com.smartscan.app.smartscanapp I/send: Received 8B FF
06-26 15:05:08.346 24733-24733/com.smartscan.app.smartscanapp I/test: IRDA1: Received 8B FF
06-26 15:05:10.201 24733-25852/com.smartscan.app.smartscanapp I/send: Received 8B FF
06-26 15:05:10.361 24733-24733/com.smartscan.app.smartscanapp I/test: IRDA1: Received 8B FF
06-26 15:05:12.220 24733-25852/com.smartscan.app.smartscanapp I/send: Received 8B FF
06-26 15:05:12.392 24733-24733/com.smartscan.app.smartscanapp I/test: IRDA1: Received 8B FF
06-26 15:05:14.236 24733-25852/com.smartscan.app.smartscanapp I/send: Received 8B FF
06-26 15:05:14.429 24733-24733/com.smartscan.app.smartscanapp I/test: IRDA1: Received 8B FF
06-26 15:05:16.255 24733-25852/com.smartscan.app.smartscanapp I/send: Received 86 C0
06-26 15:05:18.274 24733-25852/com.smartscan.app.smartscanapp I/send: Received 86 C0
06-26 15:05:18.447 24733-24733/com.smartscan.app.smartscanapp I/test: IRDA1: Received 86 C0
06-26 15:05:20.293 24733-25852/com.smartscan.app.smartscanapp I/send: Received 86 C0
06-26 15:05:22.312 24733-25852/com.smartscan.app.smartscanapp I/send: Received 86 C0
06-26 15:05:22.482 24733-24733/com.smartscan.app.smartscanapp I/test: IRDA1: Received 86 C0
06-26 15:05:24.330 24733-25852/com.smartscan.app.smartscanapp I/send: Received 86 C0
06-26 15:05:24.468 24733-24733/com.smartscan.app.smartscanapp I/test: IRDA1: Rece
06-26 15:05:26.352 24733-25852/com.smartscan.app.smartscanapp I/send: Received 86 C0
06-26 15:05:26.518 24733-24733/com.smartscan.app.smartscanapp I/test: IRDA1: Received 86 C0
06-26 15:05:28.371 24733-25852/com.smartscan.app.smartscanapp I/send: Received 86 C0
06-26 15:05:28.543 24733-24733/com.smartscan.app.smartscanapp I/test: IRDA1: Received 86 C0
06-26 15:05:30.388 24733-25852/com.smartscan.app.smartscanapp I/send: Received 86 C0
06-26 15:05:32.408 24733-25852/com.smartscan.app.smartscanapp I/send: Received 86 C0
06-26 15:05:32.577 24733-24733/com.smartscan.app.smartscanapp I/test: IRDA1: Received 86 C0
06-26 15:05:34.425 24733-25852/com.smartscan.app.smartscanapp I/send: Received 86 C0
06-26 15:05:36.444 24733-25852/com.smartscan.app.smartscanapp I/send: Received 86 C0
06-26 15:05:36.588 24733-24733/com.smartscan.app.smartscanapp I/test: IRDA1: Received 86 C0
06-26 15:05:38.464 24733-25852/com.smartscan.app.smartscanapp I/send: Received 86 C0
06-26 15:05:38.611 24733-24733/com.smartscan.app.smartscanapp I/test: IRDA1: Received 86 C0
06-26 15:05:40.482 24733-25852/com.smartscan.app.smartscanapp I/send: Received 86 C0
06-26 15:05:40.642 24733-24733/com.smartscan.app.smartscanapp I/test: IRDA1: Received 86 C0
06-26 15:05:42.500 24733-25852/com.smartscan.app.smartscanapp I/send: Received 86 C0
06-26 15:05:44.518 24733-25852/com.smartscan.app.smartscanapp I/send: Received 86 C0
06-26 15:05:46.537 24733-25852/com.smartscan.app.smartscanapp I/send: Received 86 C0
06-26 15:05:48.555 24733-25852/com.smartscan.app.smartscanapp I/send: Received 86 C0
06-26 15:05:50.574 24733-25852/com.smartscan.app.smartscanapp I/send: Received 86 C0
06-26 15:05:52.593 24733-25852/com.smartscan.app.smartscanapp I/send: Received 86 C0
06-26 15:05:52.756 24733-24733/com.smartscan.app.smartscanapp I/test: IRDA1: Received 86 C0
06-26 15:05:54.612 24733-25852/com.smartscan.app.smartscanapp I/send: Received 86 C0
06-26 15:05:54.790 24733-24733/com.smartscan.app.smartscanapp I/test: IRDA1: Received 86 C0
06-26 15:05:56.630 24733-25852/com.smartscan.app.smartscanapp I/send: Received 86 C0
06-26 15:05:56.791 24733-24733/com.smartscan.app.smartscanapp I/test: IRDA1: Received 86 C0
06-26 15:05:58.649 24733-25852/com.smartscan.app.smartscanapp I/send: Received 86 C0
06-26 15:06:00.667 24733-25852/com.smartscan.app.smartscanapp I/send: Received 86 C0
06-26 15:06:00.829 24733-24733/com.smartscan.app.smartscanapp I/test: IRDA1: Received 86 C0
06-26 15:06:02.685 24733-25852/com.smartscan.app.smartscanapp I/send: Received 86 C0
06-26 15:06:04.703 24733-25852/com.smartscan.app.smartscanapp I/send: Received 86 C0
06-26 15:06:04.868 24733-24733/com.smartscan.app.smartscanapp I/test: IRDA1: Received 86 C0
06-26 15:06:06.720 24733-25852/com.smartscan.app.smartscanapp I/send: Received 86 C0
06-26 15:06:06.900 24733-24733/com.smartscan.app.smartscanapp I/test: IRDA1: Received 86 C0
06-26 15:06:08.739 24733-25852/com.smartscan.app.smartscanapp I/send: Received 86 C0
06-26 15:06:10.760 24733-25852/com.smartscan.app.smartscanapp I/send: Received 86 C0
06-26 15:06:12.778 24733-25852/com.smartscan.app.smartscanapp I/send: Received 86 C0
06-26 15:06:12.951 24733-24733/com.smartscan.app.smartscanapp I/test: IRDA1: Received 86 C0
06-26 15:06:14.962 24733-25852/com.smartscan.app.smartscanapp I/send: Received 86 C0
06-26 15:06:16.980 24733-25852/com.smartscan.app.smartscanapp I/send: Received 86 C0
06-26 15:06:17.149 24733-24733/com.smartscan.app.smartscanapp I/test: IRDA1: Received 86 C0
06-26 15:06:18.999 24733-25852/com.smartscan.app.smartscanapp I/send: Received 86 C0
06-26 15:06:19.142 24733-24733/com.smartscan.app.smartscanapp I/test: IRDA1: Received 86 C0
06-26 15:06:21.018 24733-25852/com.smartscan.app.smartscanapp I/send: Received 86 C0
06-26 15:06:23.029 24733-25852/com.smartscan.app.smartscanapp I/send: Received 86 C0
06-26 15:06:25.043 24733-25852/com.smartscan.app.smartscanapp I/send: Received 86 C0
06-26 15:06:27.062 24733-25852/com.smartscan.app.smartscanapp I/send: Received 86 C0
06-26 15:06:27.219 24733-24733/com.smartscan.app.smartscanapp I/test: IRDA1: Received 86 C0
06-26 15:06:29.081 24733-25852/com.smartscan.app.smartscanapp I/send: Received 86 C0
06-26 15:06:29.247 24733-24733/com.smartscan.app.smartscanapp I/test: Received 86 C0
06-26 15:06:31.099 24733-25852/com.smartscan.app.smartscanapp I/send: Received 86 C0
06-26 15:06:31.277 24733-24733/com.smartscan.app.smartscanapp I/test: IRDA1: Received 86 C0
06-26 15:06:33.111 24733-25852/com.smartscan.app.smartscanapp I/send: Received 86 C0
06-26 15:06:33.299 24733-24733/com.smartscan.app.smartscanapp I/test: IRDA1: Received 86 C0
06-26 15:06:35.130 24733-25852/com.smartscan.app.smartscanapp I/send: Received 86 C0
06-26 15:06:37.148 24733-25852/com.smartscan.app.smartscanapp I/send: Received 86 C0
06-26 15:06:39.166 24733-25852/com.smartscan.app.smartscanapp I/send: Received 86 C0
06-26 15:06:39.309 24733-24733/com.smartscan.app.smartscanapp I/test: IRDA1: Received 86 C0
06-26 15:06:41.186 24733-25852/com.smartscan.app.smartscanapp I/send: Received 86 C0
06-26 15:06:41.365 24733-24733/com.smartscan.app.smartscanapp I/test: IRDA1: Received 86 C0
06-26 15:06:43.204 24733-25852/com.smartscan.app.smartscanapp I/send: Received 86 C0

仅供参考:command.getCommandReturn()刚刚收到我正在讨论的变量。

感谢。

更新

我应该清除我的坏事。

Message是活动中类的开头声明的字符串变量。

 public void onEventMainThread(TemplateMessageSystem event) {

        Log.i("test", event.getMessage());
        message = event.getMessage();
    }

这是一个消息接收器,用于处理接收传入消息的不同线程。它确实正确地设置了消息变量以及它应该是什么。我不确定字符串比较的加载时间,但这种方法每1-2秒可能有2个字符串。

0 个答案:

没有答案