Preventing Internet-accessing-method from delaying a toast popup

☆樱花仙子☆ 提交于 2019-12-23 01:45:50

问题


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


回答1:


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();
        }
    }
}



回答2:


You shouldn't be performing long running (ie. network or disk I/O) operations in the UI thread. You should use an AsyncTask or a Thread/Handler combination.

Here are some links:

  • Painless Threading
  • AsyncTask
  • Thread


来源:https://stackoverflow.com/questions/4828676/preventing-internet-accessing-method-from-delaying-a-toast-popup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!