Ia m Using HandlerThread and Handler in my below code, Actually I try to update the data from runnable to Handler callback.
In every loop the sendMessage() function Called but I didn't received nothhing in Callback. What I am missing?
public class MainActivity extends AppCompatActivity {
private TextView result;
HandlerThread handlerThread = new HandlerThread("bThread");
Callback callBack = new Callback(){
@Override
public boolean handleMessage(Message message) {
Bundle bundle = message.getData();
String val = bundle.getString("output");
result.setText(val);
return false;
}
};
private Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
result = findViewById(R.id.result);
handlerThread.start();
handler = new Handler(handlerThread.getLooper(),callBack);
handler.post(runnable);
}
Runnable runnable= new Runnable() {
@Override
public void run() {
for (int i = 0; i <200 ; i++) {
try {
Message message = new Message();
Bundle bundle = new Bundle();
bundle.putString("output",i+"");
message.setData(bundle);
handler.sendMessage(message);
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
}