sriram301296
Lurker
Using an Async task as follows:
At this part of execution:
This strange problem does not occur, if the whole code is run directly on UI Thread and the graph is rendered perfectly. In that case, as you know, the UI gets blocked for a few seconds.
I tried, instead of AsyncTask running the code as an individual thread but the same results persist. Any workarounds can be made? Can a service be used in this case?
private class ProcessFileTask extends AsyncTask<Void, byte[], Void>{
String fileName;
BufferedReader bufferedReader;
String[] splitData;
byte[] currentArray;
ByteBuffer bb;
public ProcessFileTask(String fileName) {
this.fileName = fileName;
currentArray = new byte[19];
}
@override
protected Void doInBackground(Void... params) {
try {
bufferedReader = new BufferedReader(new FileReader(fileName));
splitData = bufferedReader.readLine().split(" ");
for (int i = 0; i < splitData.length; i += 19) {
for (int j = 0; j < 19; j++) {
currentArray[j] = Byte.parseByte(splitData[i+j]);
}
publishProgress(currentArray);
}
}
catch (Exception e){
Log.e("Exception occured", e.toString());
}
return null;
}
@override
protected void onProgressUpdate(byte[]... values) {
super.onProgressUpdate(values);
bb = ByteBuffer.allocate(20);
bb.order(ByteOrder.LITTLE_ENDIAN);
bb.put(values[0], 1, 18);
ChannelOne.updateGraph(bb.getShort(0), bb.getShort(2), bb.getShort(4));
ChannelTwo.updateGraph(bb.getShort(12), bb.getShort(14), bb.getShort(16));
}
}
String fileName;
BufferedReader bufferedReader;
String[] splitData;
byte[] currentArray;
ByteBuffer bb;
public ProcessFileTask(String fileName) {
this.fileName = fileName;
currentArray = new byte[19];
}
@override
protected Void doInBackground(Void... params) {
try {
bufferedReader = new BufferedReader(new FileReader(fileName));
splitData = bufferedReader.readLine().split(" ");
for (int i = 0; i < splitData.length; i += 19) {
for (int j = 0; j < 19; j++) {
currentArray[j] = Byte.parseByte(splitData[i+j]);
}
publishProgress(currentArray);
}
}
catch (Exception e){
Log.e("Exception occured", e.toString());
}
return null;
}
@override
protected void onProgressUpdate(byte[]... values) {
super.onProgressUpdate(values);
bb = ByteBuffer.allocate(20);
bb.order(ByteOrder.LITTLE_ENDIAN);
bb.put(values[0], 1, 18);
ChannelOne.updateGraph(bb.getShort(0), bb.getShort(2), bb.getShort(4));
ChannelTwo.updateGraph(bb.getShort(12), bb.getShort(14), bb.getShort(16));
}
}
At this part of execution:
for (int i = 0; i < splitData.length; i += 19) {
for (int j = 0; j < 19; j++) {
currentArray[j] = Byte.parseByte(splitData[i+j]);
}
publishProgress(currentArray);
}
I expect the publishProgress to be called everytime the 'i' loop executes. While that happens(which was verified by Logging data), the code in onProgressUpdate() does not get executed properly(which is evident because the result of updateGraph can be seen only after the AsyncTask ends. Also, the end graph is completely mis-formed(the graph shows mostly straight lines while it must be curves).for (int j = 0; j < 19; j++) {
currentArray[j] = Byte.parseByte(splitData[i+j]);
}
publishProgress(currentArray);
}
This strange problem does not occur, if the whole code is run directly on UI Thread and the graph is rendered perfectly. In that case, as you know, the UI gets blocked for a few seconds.
I tried, instead of AsyncTask running the code as an individual thread but the same results persist. Any workarounds can be made? Can a service be used in this case?