简体   繁体   English

正确使用AsyncTasks并处理View和Activity所需的对象

[英]Correct use of AsyncTasks and handle Objects needed by a View and Activity

So I am currently writing an App and have a few questions about how to deal with AsyncTasks. 因此,我目前正在编写一个App,并且对如何处理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 . MainActivity-此活动仅执行setContentView(R.layout.myLayout)并创建我的CustomLocationListener的对象。 The Layout contains a custom view and a ProgressBar . 布局包含一个自定义视图和一个ProgressBar
  2. CustomLocationListener - This is basically just a Class which implements LocationListener and has the default LocationListener methods. CustomLocationListener-这基本上只是一个实现LocationListener并具有默认LocationListener方法的类。 In addition to that I wrote a method to check which providers are enabled to prior use the NETWORK_PROVIDER . 除此之外,我还编写了一种方法来检查启用了哪些提供程序的事前可以使用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. MyCustomView-此类扩展了View类。 It also has an Attribute of the type Display (my own class). 它还具有类型Display (我自己的类)的Attribute。

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: 好吧,我现在的问题是,如何处理AsyncTasks,因为这是App应该执行的步骤:

  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) 对数据库服务器(mysql)执行一些查询,然后将其永久保存(数据由城市名称组成)
  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. 我的意思是,两个计算(第3步和第4步)必须彼此了解。 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. 步骤4的计算需要步骤3的计算结果。我的Activity类还应该知道计算状态,因为在开始绘制时必须使ProgressBar不可见。

I hope I was detailed enough and I appreciate your help! 希望我足够详细,感谢您的帮助!

Well, from the AsyncTask docs you can gather this: 好了,您可以从AsyncTask文档中收集以下信息:

When an asynchronous task is executed, the task goes through 4 steps: 当执行异步任务时,该任务将经历4个步骤:

  1. onPreExecute(), invoked on the UI thread before the task is executed. onPreExecute(),在执行任务之前在UI线程上调用。 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. doInBackground(Params ...),在onPreExecute()完成执行后立即在后台线程上调用。 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. 此步骤还可以使用publishProgress(Progress ...)发布一个或多个进度单位。 These values are published on the UI thread, in the onProgressUpdate(Progress...) step. 这些值在onProgressUpdate(Progress ...)步骤中发布在UI线程上。

  3. onProgressUpdate(Progress...), invoked on the UI thread after a call to publishProgress(Progress...). onProgressUpdate(Progress ...),在调用publishProgress(Progress ...)之后在UI线程上调用。 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. onPostExecute(Result),在后台计算完成后在UI线程上调用。 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. 因此,我建议您使用两个AsyncTasks在doInBackground(Params...)方法中完成地理和服务器工作(每个工作doInBackground(Params...)然后在onPostExecute(Result)方法中将值返回给MainActivity。 Also, keep your MainActivity aware of the progress by using the onProgressUpdate(Progress..) method. 另外,通过使用onProgressUpdate(Progress..)方法使MainActivity知道进度。

When they are done, you can choose to use another AsyncTask to do the calculations if they stall the UI thread noticeably. 完成后,如果它们显着停止了UI线程,则可以选择使用另一个AsyncTask进行计算。 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. 当您准备显示计算所得的内容时,还有一个不可见的视图,并准备好在其上绘制时将其设置为可见。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM