I am struggling with setting the text of a TextView, so I am now trying to do it with a button push, but when I start the thread from the button readWeight
the button updateButton
does not work.
Here are my two button onClick
methods:
readWeight.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
inputWindow.setText("helloooooooo worldddddd");
//connector.run();
System.out.println("********** PRINTING **********");
// readWeight.setVisibility(View.INVISIBLE);
}
});
updateButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
System.out.println("!!!!!!!!!!!!!!!"+weight+"!!!!!!!!!!!!!!!");
inputWindow.setText(weight);
}
});
and here is my method that starts the thread, this method is in another class:
public void run() {
new Handler().post(new Runnable() {
@Override
public void run() {
// Always cancel discovery because it will slow down a connection
//Log.d("workkkkkk","$$$$$$$$$$$$$$$$****** printingggggg ******$$$$$$$$$$$$$$$$");
int counter = 0;
while (true) {
counter++;
try {
output = "";
//read the data from socket stream
//mmInStream != null && counter%10000000 == 1
if (mmInStream != null) {
mmInStream.read(buffer);
for (byte b : buffer) {
char c = (char) b;
if (c >= ' ' && c < 'z') {
// System.out.print(c);
output += c;
}
}
System.out.println();
Intent intent = new Intent();
intent.setAction("com.curie.WEIGHT_RECEIVED");
intent.putExtra("Output",output);
if (counter % 10 == 0) {
System.out.println(counter);
//InputActivity.setInputWindowText(output);
LocalBroadcastManager.getInstance(InputActivity.getContext()).sendBroadcastSync(intent);
}
}
// Send the obtained bytes to the UI Activity
} catch (IOException e) {
//an exception here marks connection loss
//send message to UI Activity
break;
}
}
}
Any help would be very appreciated! thank you.
When you use
Handler.post()
It runs in UI thread, so if it is long action it will block all interface. To avoid it you should run it in another thread. If you don't want to use something complicated you can try this:
mHandler = new Handler();
new Thread(new Runnable() {
@Override
public void run () {
mHandler.post(new Runnable() {
@Override
public void run () {
// place your action here
}
});
}
}).start();