Bobbie wrote:
> Hey all, thanks for all your help so far! I have the following
> function running on a timer every 20 seconds or so. However, every
> time it runs, my user interface for this program (an EditText box and
> a submit Button) freezes until the HttpPost is finished. Is there a
> way for me to run this function "in the background" or something so it
> doesn't freeze the interface? Is there a more efficient way to do
> what I'm trying to do here? The "chatscreen" variable is a TextView.
Yes, run it in another thread --- look up java.lang.Thread. Be aware
that methods in another thread can't call anything in the UI directly,
though; look up Handler.
void doSomethingInBackground()
{
/* Here we're in the UI thread. */
final Handler handler = new Handler();
Thread thread = new Thread()
{
public void run()
{
/* Here we're in the background thread. */
/* perform blocking operation here */
handler.post(
new Runnable()
{
public void run()
{
/* Back in the UI thread again. Tell the user we're
* finished. */
}
}
);
}
};
/* Start background thread, don't wait (will return immediately). */
thread.start();
}
If you think the syntax is nasty, you're not the only one. Bear in mind
that your activity may have finished by the time the thread completes.
Also, if you're going to do this a lot, you'll want to reuse the Handler
and Thread objects.
--
David Given
[EMAIL PROTECTED]
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---