preventing互联网访问法,从延迟举杯弹出

编程入门 行业动态 更新时间:2024-10-28 09:22:36
本文介绍了preventing互联网访问法,从延迟举杯弹出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

我工作的一个Android应用程序,涉及到获取网页字符串,使用基于可用的 www.spartanjava/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 www.spartanjava/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("www.google/"); 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[] {"google/"}); } 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互联网访问法,从延迟举杯弹出

本文发布于:2023-10-06 19:49:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1467652.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:互联网   弹出   preventing

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!