My application gets data every 80 milliseconds (I asked my boss today and he said 80 ms is okay, but perfect would be 2ms - 20ms, so even faster than I thought). I need to work with the messages that are incoming, so I tried to handle them with if clauses and every time the if clause is true there is a new text set in a TextView
like that:
if (data.equalsIgnoreCase(test)) {
TextView tv = (TextView) findViewById(R.id.textview);
tv.setText(" 30 % ");
}
The problem is, that the data are incoming that fast and the if clauses seem not "fast" enough to handle it.
the TextView
only changes when I stop the transmission of messages, then after a short "waiting" the TextView
changes.
Is there something to make it more "Realtime" ?
I didn't find something helpful via google and my programming skills are very weak only to prevent more downvotes
EDIT:
I now found out, that I have to check 768 different values (all hexcodes). So there would be 768 if-statements. That would definetly break down the speed.
Problem is, I cant "wait" to do first the if-statement and then get the next message.
The messages just flow in.
I just updated my code and use now ProgressBar
instead of a TextView
if (data.equalsIgnoreCase(TEST)){
mProgressBar.setProgress(0);
}
EDIT2: Added Thread
code
class RxThread extends Thread {
private static final String TEST = "0 0 0 0 0 40 0 0 ";
private static final String TEST2 = "0 1 0 0 0 41 0 0 ";
private static final String TEST3 = "2 bb 0 0 2 fb 0 0 ";
private static final String TEST4 = "2 bd 0 0 2 fd 0 0 ";
private static final String TEST5 = "2 be 0 0 2 fe 0 0 ";
Handler myHandler = new Handler();
final Runnable r = new Runnable() {
public void run() {
demoRxMsg = new MessageStructure();
while (rxChannel.receiveMessage(demoRxMsg) == ReturnCode.SUCCESS) {
String data = "";
String format = "";
rxChannel.receiveMessage(demoRxMsg);
if (demoRxMsg.frameFormat == API_ADK.STANDARD_FRAME) {
format = "SFF";
} else {
format = "EFF";
}
for (byte i = 0; i < demoRxMsg.dataLength; i++) {
data = data + Integer.toHexString(demoRxMsg.data[i]) + " ";
}
if (data.equalsIgnoreCase(TEST)){
mProgressBar.setProgress(0);
} else if (data.equalsIgnoreCase(TEST2)) {
mProgressBar.setProgress(1);
} else if (data.equalsIgnoreCase(TEST3)) {
mProgressBar.setProgress(94);
} else if (data.equalsIgnoreCase(TEST4)) {
mProgressBar.setProgress(95);
} else if (data.equalsIgnoreCase(TEST5)) {
mProgressBar.setProgress(96);
}
}
}
}
};
public void run() {
while (true) {
try {
Thread.sleep(60);
myHandler.post(r);
} catch (InterruptedException e) {
break;
}
}
}
How to make it perform better?
EDIT 3:
For better explanation how fast it has to be:
At the moment I'm receiving every 80 millisecond a message in hex format. The message object has 5 items: frame format, data format, data length, message ID and data. In perfect circumstances I get one every 2-20 ms.
There are 768 different messages I have to differentiate. those messages are devided by 200 (to get 0.5% steps).
What I want is a ProgressBar
that changes and runs fluently as the hexcodes "rain" in and the percentage status changes.
One problem is, that I have no influence on how fast the messages are received. Its always the same speed. Is it even possible to process the data that fast?