views:

146

answers:

2

I have a problem. 1. I have two threads 'worker' and 'UI' thread. 2. worker keep on waiting for data from server, when gets it notifies to UI thread. 3. On update UI Toast a msg on screen. Step 3 is problem as it says

android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

using mHandler, runOnUIThread slows down the UI thread (UI displays webview), as I have to continuously check for data from server.

Thanks in advance.

+1  A: 

Use AsyncTask to implement this. Override doInBackground to get the data (it is executed on the separate thread), then override onPostExecute() to show the toast (it is executed on the UI thread).

Here is good example http://www.screaming-penguin.com/node/7746

And here is javadoc http://developer.android.com/intl/zh-TW/reference/android/os/AsyncTask.html

UPD: Example on how to handle partial progress.

    class ExampleTask extends AsyncTask<String, String, String>{

    @Override
    protected String doInBackground(String... params) {
        while(true){
            //Some logic on data recieve..
            this.publishProgress("Some progress");
            //seee if need to stop the thread.
            boolean stop = true;
            if(stop){
                break;
            }
        }
        return "Result";
    }

    @Override
    protected void onProgressUpdate(String... values) {
        super.onProgressUpdate(values);
        //UI tasks on particular progress...
    }
}
Konstantin Burov
Thank you for reply,actually data keeps on coming in some interval from server and again I have to update the UI. In this example, i guess doInbackground is executed only when button is clicked but in my case someone(thread) should always be ready to receive data from server and pass to UI to update itself. Meanwhile UI sud work normally (webview)....
Placidnick
you can invoke publishProgress every time you get another portion of data. See the updated answer.
Konstantin Burov
ohhh wow......i am your big time fan :)Konstantin rocks!!!Thank you.
Placidnick
1 more problem:1. I have another chatActivity which displays chat.Now if I receive chat data from server, content in chatActivity should be updated.2. Can it be done with the same doInBackground in UI Activity or I should have a service for updating both activity.3. If service, than can i bind 2 activity with 1 service i.e. if user press logout from UI or chatActivity, both activity and service should die.I am new to 'service' so any reference or sample code wud b helpful.Thank you for your consideration :)
Placidnick
A: 

I would use a service, and bind your activity to the service. Then the service can send a broadcast when it has new data

Falmarri
can you please suggest me sample code or some reference, it would be way helpful for me. As I have to implement chat1. if user is on another activity and comes back to chat activity he should get the updated chat.
Placidnick