Search code examples
javaandroiddelaytoast

Preventing Internet-accessing-method from delaying a toast popup


Quite new to Android development and Java in general, so please excuse any amateur ignorance and lack of terminology.

I'm working on an Android app that involves fetching Web pages as strings, using a method based on the code available at http://www.spartanjava.com/2009/get-a-web-page-programatically-from-android/.

This takes a small but noticeable amount of time, but works fine. It is triggered by pressing a button in the UI. Since the application is unresponsive while data is being fetched, I've got a toast that is meant to warn users before it happens.

Here is essentially what is being done (not the actual code, just illustrative):

public void buttonPressed(View view) 
{
   Toast.makeText(this, "Getting Data!", Toast.LENGTH_LONG).show();

   //See the page linked above for the code in this function!
   String page = getPage("http://www.google.com/");

   Toast.makeText(this, "Data Retrieved!", Toast.LENGTH_LONG).show();
}

Unfortunately, The "Getting Data" toast only seems to appear after the getPage method has completed, appearing very briefly before being covered up by the "Data Retrieved" toast.

How do I avoid this, making the "Getting Data" toast appear, then the getPage method run, then the "Data Retrieved" toast appear when the method terminates?

Any suggestions would be much appreciated. I expect the solution involves some kind of threads or synchronisation, but don't even know where to start looking for an appropriate tutorial...

Greg


Solution

  • correct use of an AsyncTask class that solves your problem:

    notice the onPreExecute and onPostExecute methods which are called before/after your get the page.

    public class HomeActivity extends Activity {
        public void onCreate(Bundle icicle) {
            super.onCreate(icicle);
            setContentView(R.layout.home);
        }
        public void buttonPressed(View view) {
            new MyAsyncTask(this).execute(new String[] {"http://google.com/"});
        }
        private class MyAsyncTask extends AsyncTask<String, Void, String> {
            private Context context;
            public MyAsyncTask(Context context) {
                this.context = context;
            }
            @Override
            protected String doInBackground(String... params) {
                String page = getPage(params[0]);
                        //do any more work here that may take some time- like loading remote data from a web server, etc
                return page;
            }
            @Override
            protected void onPostExecute(String result) {
                super.onPostExecute(result);
                Toast.makeText(context, "Data Retrieved: " + result, Toast.LENGTH_LONG).show();
            }
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                Toast.makeText(context, "Getting Data!", Toast.LENGTH_LONG).show();
            }
        }
    }