正确使用AsyncTasks和处理由一个观活动需要的对象 [英] Correct use of AsyncTasks and handle Objects needed by a View and Activity

查看:189
本文介绍了正确使用AsyncTasks和处理由一个观活动需要的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我目前正在写一个应用程序,并有关于如何处理AsyncTasks几个问题。
我描述了我的问题尽可能详细,我可以。我有以下类:


  1. MainActivity - 此活动仅执行的setContentView(R.layout.myLayout)
    创建我的 CustomLocationListener 的对象。布局包含自定义视图和进度

  2. CustomLocationListener - 这基本上只是一个实现类 LocationListener的和具有默认 LocationListener的方法。另外,我写了一个方法来检查哪些供应商被允许之前使用 NETWORK_PROVIDER

  3. 显示 - 这只是一个assisstant类。我用它只得到显示指标来计算一些东西。

  4. MyCustomView - 这个类继承了查看类。它还具有类型显示(我自己的类)的属性。

现在你应该知道我的code一切都重要。好了,现在我的问题是,如何处理AsyncTasks,因为这是具体步骤的应用程序应该通过:


  1. 获取用户设备的当前位置

  2. 执行一些查询的数据库服务器(MySQL的),并将其保存非持久(数据由城市名)

  3. 计算在数据库中的用户位置和城市之间的距离

  4. 请一些其他的计算,以计算出的位置在哪里画在屏幕上

  5. 绘制一切

在的,我想实现一个进度条,这应该是积极的,直到所有的计算都完成,图纸阶段开始前。
所以,我有点困惑在哪里code什么。我的意思是,这两个计算(步骤3和4)必须了解对方。第4步的计算需要第3步的计算结果和我的Activity类还应该了解计算的状态,因为我必须使进度看不见,当我开始绘制。

我希望我是不够详细,我AP preciate您的帮助!


解决方案

好了,从的 AsyncTask的文档可以收集这样的:


  

当执行一个异步任务,任务经过4个步骤:


  
  

      
  1. 在preExecute(),执行任务前的UI线程调用。此步骤通常是用于设置任务,例如
      通过显示在用户界面中的进度条。


  2.   
  3. doInBackground(参数...),在后台线程中调用后,随即于preExecute()执行完毕。此步骤用于
      执行后台计算,并可以花费很长时间。该
      异步任务的参数传递到这一步。该
      计算结果必须在这步返回,将是
      传递回的最后一步。此步骤也可以使用
      publishProgress(进展...)公布的进度一个或多个单元。
      这些值公布在UI线程上,在
      onProgressUpdate(进展...)的一步。


  4.   
  5. onProgressUpdate(进展...),以publishProgress(进步...)的调用后调用UI线程。执行的定时是
      未定义。此方法用于显示的任何形式的进展
      而后台计算仍在执行的用户界面。
      例如,它可用于在动画进度条或显示日志
      文本字段。


  6.   
  7. onPostExecute(结果),调用后台计算结束后在UI线程上。背景的结果
      计算被传递给此步骤作为一个参数


  8.   

那么,我建议你的是使用两个AsyncTasks做你的地理和服务器(每个)在 doInBackground(参数...)工作方法和值返回到 onPostExecute(结果)你MainActivity 方法。此外,通过使用 onProgressUpdate(进展..)方法,让你的MainActivity知道的进度。

当他们这样做时,可以选择使用其他的AsyncTask来做计算,如果他们拖延UI线程显着。使用执行相同的方法吧。

当这一切完成你准备画一个观点......(我想是你在说什么)

有一个默认视图负载启动,上面写着正在加载...或任何计算之前。有另一种观点,是不可见的准备,当你​​准备好你的显示计算的东西,只是设置时可见它已准备好要绘制上。

So I am currently writing an App and have a few questions about how to deal with AsyncTasks. I describe my problem as detailed as I can. I have the following classes:

  1. MainActivity - This Activity only executes setContentView(R.layout.myLayout) and creates an Object of my CustomLocationListener. The Layout contains a custom view and a ProgressBar.
  2. CustomLocationListener - This is basically just a Class which implements LocationListener and has the default LocationListener methods. In addition to that I wrote a method to check which providers are enabled to prior use the NETWORK_PROVIDER.
  3. Display - This is just an assisstant class. I use it only to get display metrics to calculate some things.
  4. MyCustomView - This class extends the View class. It also has an Attribute of the type Display (my own class).

Now you should know everything important to my code. Well, my question now is, how to deal with AsyncTasks, since this are the steps the App should go through:

  1. Get the current location of the users device
  2. Execute some queries to a database server (mysql) and save them non persistent (the data consists of city names)
  3. Calculate the distance between the users location and the cities in the database
  4. Do some other calculations to figure out the positions where to draw on the screen
  5. Draw everything

On top of that I would like to implement a progress bar, which should be active untill all calculations are done and the drawing phase begins. So, I'm kinda confused where to code what. I mean, both calculations (step 3. and 4.) have to know about each other. The calculation of step 4 needs the results of the calculation of step 3. And my Activity class should also know about the state of the calculations, since I have to make the ProgressBar invisible when I start to draw.

I hope I was detailed enough and I appreciate your help!

解决方案

Well, from the AsyncTask docs you can gather this:

When an asynchronous task is executed, the task goes through 4 steps:

  1. onPreExecute(), invoked on the UI thread before the task is executed. This step is normally used to setup the task, for instance by showing a progress bar in the user interface.

  2. doInBackground(Params...), invoked on the background thread immediately after onPreExecute() finishes executing. This step is used to perform background computation that can take a long time. The parameters of the asynchronous task are passed to this step. The result of the computation must be returned by this step and will be passed back to the last step. This step can also use publishProgress(Progress...) to publish one or more units of progress. These values are published on the UI thread, in the onProgressUpdate(Progress...) step.

  3. onProgressUpdate(Progress...), invoked on the UI thread after a call to publishProgress(Progress...). The timing of the execution is undefined. This method is used to display any form of progress in the user interface while the background computation is still executing. For instance, it can be used to animate a progress bar or show logs in a text field.

  4. onPostExecute(Result), invoked on the UI thread after the background computation finishes. The result of the background computation is passed to this step as a parameter.

So what I would suggest for you is to use two AsyncTasks to do your geo and server (one for each) work in the doInBackground(Params...) method and return the value to your MainActivity in the onPostExecute(Result) method. Also, keep your MainActivity aware of the progress by using the onProgressUpdate(Progress..) method.

When they are done, you can choose to use another AsyncTask to do the calculations if they stall the UI thread noticeably. Use the same method of execution for it.

When this is all done you are ready to draw a view... (I think is what you're saying)

Have a default view load before you start the calculations that says "Loading..." or whatever. Have another view that is not visible ready for when you are ready to display your calculated things and just set it visible when it is ready to be drawn on.

这篇关于正确使用AsyncTasks和处理由一个观活动需要的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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