I'm trying to send data to my Thread called ToD1 which is in an inner class of my Threads class then I need to do some work to it then need it to be set back to the UI thread handler my qestion is how would I call the Handlers to send the Message data? I know about AsyncTask and will try using it later the purpose of this app is for me to learn and understand the concepts of Handlers, HandlerThread, and AsyncTask so I can use them in my programing.
Here is my code for MainActivity:
public class MainActivity extends Activity implements OnClickListener {
TextView display1;
TextView display2;
EditText cl;
Button enter;
Button tod1;
Button tod2;
Bundle d = new Bundle();
static Handler mHandler;
Message msg;
Object task;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tod1 = (Button) findViewById(R.id.display1);
tod2 = (Button) findViewById(R.id.display2);
enter = (Button) findViewById(R.id.Enter);
display1 = (TextView) findViewById(R.id.Display1);
display2 = (TextView) findViewById(R.id.Display2);
cl = (EditText) findViewById(R.id.CL);
mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
}
};
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.Enter:
Bundle in = new Bundle();
String input = cl.getText().toString();
d.putString("in", input);
msg.setData(d);
// eclipes ask to resovle d1 varible type?
d1.sendMessage(msg);
notifyAll();
break;
case R.id.display1:
break;
case R.id.display2:
break;
}
}
}
Here is my Thread class:
public class Threads {
public static class ToD1 {
Thread display1 = new Thread() {
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
Message msg = new Message();
Bundle b = new Bundle();
Looper.prepare();
Handler d1 = new Handler() {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
}
};
String output;
b.putString("key1", "out:" + String.valueOf(output));
msg.setData(b);
// Send msg to UI thread handler
// eclipes asked solve mHandler to varible type
mHandler.sendMessage(msg);
}
};
}
}