preventing互联网访问法,从延迟举杯弹出 [英] Preventing Internet-accessing-method from delaying a toast popup

查看:191
本文介绍了preventing互联网访问法,从延迟举杯弹出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很新的一般的Andr​​oid开发和Java,所以请原谅术语的任何业余的无知和缺乏。

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

我工作的一个Android应用程序,涉及到获取网页字符串,使用基于可用的 http://www.spartanjava.com/2009/get-a-web-page-programatically-from-android/

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/.

这需要时间小,但明显的数额,但工作正常。它是由pressing在UI按钮触发。由于应用程序没有响应,在数据被牵强,我有一个,就是在它发生之前警告用户敬酒。

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.

下面基本上正在做什么(而不是实际的code,只是举例):

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

不幸的是,获取数据敬酒似乎只出现在GETPAGE方法完成后,被覆盖了由数据取自敬酒出庭很简单。

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.

我如何避免这种情况,使得获取数据敬酒出现,那么GETPAGE方法运行,那么数据取自敬酒时,方法终止出现?

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?

任何建议将是多少AP preciated。我期望的解决方案涉及某种线索或同步的,但甚至不知道从哪里开始寻找一个合适的教程...

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...

格雷格

推荐答案

正确使用,解决你的问题的的AsyncTask 类:

correct use of an AsyncTask class that solves your problem:

注意上preExecute onPostExecute 方法之后,被称为前/你得到的页面。

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

这篇关于preventing互联网访问法,从延迟举杯弹出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆