Hi,
The parameters can be anything you like, think in terms of what kinds of
values are needed for your background task to do its work, and what kind
of value it returns.
I think AsyncTask<Void, String, Void> matches your code.
AsyncTask manages a background thread, and facilitates passing status
messages from the background thread into the UI thread, within the same
object (your AsyncTask subclass).
So, for your server, run your networking loop inside doInBackground(),
publish status messages from the background thread by calling
publishProgress(msg), and append them to the text view in
onProgressUpdate(String msg).
Make sure to read about handling screen rotations when using AsyncTask
(was discussed on this list).
-- Kostya
17.10.2010 1:53, chcat пишет:
I do not see how it can work - my thread is asynchronous task blocked
on "receive" method
The thread must initiate update of textview
i had a look at the AsyncTask but what parameters should be passed to
it?
Thanks again,
-V
On Oct 16, 5:03 pm, Kostya Vasilyev<[email protected]> wrote:
Runnable is a Java interface that defines one method: "run". You can
think of it as a method packaged as an object.
Define a class that implements Runnable, and pass it to m_view.update()
or runOnUIThread().
Once you've done that, the runnable object's run() method will be called
(asynchronously) on the UI thread. This method can then call methods in
m_view to display the message etc.
You might also want to look at AsyncTask:
http://developer.android.com/reference/android/os/AsyncTask.html
>>
AsyncTask enables proper and easy use of the UI thread. This class
allows to perform background operations and publish results on the UI
thread without having to manipulate threads and/or handlers.
<<
-- Kostya
17.10.2010 0:12, chcat пишет:
Ok, that helps, but what is Runnable action and how is it related to
thread?
On Oct 16, 3:00 pm, Kostya Vasilyev<[email protected]> wrote:
You can only access UI objects from the main (UI) thread.
There are several ways to "post" UI operations to the UI thread.
This one is probably the easiest: Activity.runOnUIThread(Runnable action)
http://developer.android.com/reference/android/app/Activity.html#runO...)
-- Kostya
16.10.2010 20:58, chcat пишет:
I have an Activity with TextView that I am trying to update from
different thread. To do that i pass TextView to the "updater" thread
onCreate(..)
..
txtStatus = (TextView)this.findViewById(R.id.status);
// start udp server as separate thread and pass TextView to that
thread to print text messages from udp socket
new Thread(new TelemetryServer(txtStatus)).start();
//see thread code below
As a result i have thread error, even though "updater" is
synchronized:
ERROR/UDP(282): android.view.ViewRoot$CalledFromWrongThreadException:
Only the original thread that created a view hierarchy can touch its
views.
Any other ideas how to solve this problem?
Tnank you,
-V
public class TelemetryServer implements Runnable {
public static final String SERVERIP = "127.0.0.1"; // 'Within' the
emulator!
public static final int SERVERPORT = 2222;
private TextView m_view;
private DatagramSocket socket;
private boolean loop;
private synchronized void update( String msg)
{
m_view.append("\n");
m_view.append(msg);
}
public TelemetryServer(TextView _view)
{
m_view = _view;
loop = true;
}
public void run() {
try {
InetAddress serverAddr =
InetAddress.getByName(SERVERIP);
Log.d("UDP", "S: Connecting...");
socket = new DatagramSocket(SERVERPORT,
serverAddr);
Log.i("TelemetryServer", "started");
while(loop) {
byte[] buf = new byte[1024];
DatagramPacket packet = new
DatagramPacket(buf, buf.length);
socket.receive(packet);
update(new String(packet.getData()));
}
} catch (Exception e) {
Log.e("UDP", "S: Error", e);
} // end of try
Log.d("UDP", "S: Done.");
socket.close();
}
}
--
Kostya Vasilyev -- WiFi Manager + pretty widget --http://kmansoft.wordpress.com
--
Kostya Vasilyev -- WiFi Manager + pretty widget --http://kmansoft.wordpress.com
--
Kostya Vasilyev -- WiFi Manager + pretty widget -- http://kmansoft.wordpress.com
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en