I have a MainActivity the default start up Activity which creates a TCP worker thread.
This TCP thread receives some data from the server and passes it to the current Activity on display say there are two more Activities called Activity1 and Activity2 which will display the received data.
How do i achive this using Handlers ?
Here is a outline of what I have…please suggest solution or change everything if I am completely wrong.
public class MainActivity extends Activity
{
public static TCPFunctions tcp = null;
public static Handler handler;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
handler = new Handler();
tcp = new TCPFunctions(this, handler);
tcp.start();
}
}
—————————————-TCP Thread Class———————————
public class TCPFunctions extends Thread
{
public Handler handler;
//socket and io streams are here and they work properly
Public TCPFunctions(MainActivity main, Handler _handler)
{
this.main = main;
handler = _handler;
}
public void run()
{
Intent showActivity1 = new Intent(main, Activity1.class);
main.startActivity(showActivity1);
while(true)
{
directories = new Vector<String>();
directories = (Vector<String>) inputStream.readObject();
Message msg = Message.obtain();
msg.obj = directories;
handler.sendMessage(msg);
directories = null;
}
}
}
now say in Activity1 I want this directories object…
Lets say my Activity1 has a button which when pressed sends a request to the server to get the directories object…which is received by the TCP thread…now how do I get this in Activity1 and update the UI…
Basically the directories object is a Vetor of Strings and I want to display the strings on a ListView contained in the Activity1/Activity2
public class Activity1 extends Activity implements OnClickListener,OnItemClickListener
{
private ListView directoryList;
private Button rootButton;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.fileexplorer);
directoryList = (ListView) findViewById(R.id.directories);
rootButton.setOnClickListener(this);
directoryList.setOnItemClickListener(this);
}
public void onClick(View v)
{
switch(v.getId())
{
case R.id.root_button:
Log.d(TAG, "FileExplorer: Root Button Pressed");
//request for directories made here
break;
}
}
// What should be the Handler code to get the directories ?
}
First: The handler executes runnables and handles messages in the thread that created it. So if you want Activity1 to react to data from TCPFunctions, you have two options. Either:
-MainActivity, which created the handler in your current code, needs to react to the message, get the data, and send it along to Activity1
-Or Activity1 needs to be the one to create the handler.
In either case, the core answer to your question of how to react to a sent message, is that you need to override the handleMessage() method by subclassing the handler. Here’s a boilerplate snippet you can use (pulled from one of the sample apps on the Android developer website)