如何安排在Android长(耗时)的行动? [英] How to arrange long (time consuming) actions on Android?

查看:106
本文介绍了如何安排在Android长(耗时)的行动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我们在SomeActivity和活动有一个按钮,调用移动文件从一个目录到另一个(我们称之为任务)。

For instance, we are in SomeActivity and the activity has a button that invokes moving files from one dir to another (let's call it job).

在黑莓我想:

  1. 推了不可撤销弹出(对话窗口)说:请稍候...
  2. 在启动一个线程,满足工作
  3. 在线程完成关闭弹出

本办法99.99%,可以保证我们住在同一个屏幕上后,任务已经结束,用户只要看到弹出并等待作业完成。设备旋转或其他不破所需的工作流。

This approach 99.99% can guarantee that we stay on the same screen after the task is over, user just sees the popup and waits for job completion. Device rotation or whatever does not break the desired workflow.

在Android的事情的变化。我知道有这可能是提供给解决我的情况AsyncTask的。甚至还有一个很好的<一个href="http://$c$c.google.com/p/shelves/source/browse/trunk/Shelves/src/org/curiouscreature/android/shelves/activity/ShelvesActivity.java"相对=nofollow>例如中应该如何使用。但是,由于不能保证多久的活动实例将生活的AsyncTask的应取消对的onSaveInstanceState(并重新启动在onRestoreInstanceState)。这意味着使用的AsyncTask也不能保证我们能够充分履行一旦开始工作。在某些情况下,如发送一个HTTP POST请求创建我不希望得到一个用户用户已经存在此登录上reruning的AsyncTask的麻烦。这是可能的,因为AsyncTask的可中断请求时,已经发出(和服务器实际上做的工作 - 创建一个新用户),但AsyncTask的将被取消,我们得到的回应之前

On Android things change. I know there's the AsyncTask that is probably provided to solve my case. There's even a good example of how it should be used. But since there's no guarantee of how long an Activity instance will live the AsyncTask should be cancelled on onSaveInstanceState (and restarted on onRestoreInstanceState). This means using AsyncTask there's no guarantee we are able to fully fulfill the job once started. In some cases as sending an http post request for creating a user I would not want to get in "user already exists for this login" trouble on reruning the AsyncTask. This is possible since the AsyncTask can be interrupted while the request is already sent (and the server actually is doing its job - creating a new user), but the AsyncTask is canceled before we got the response.

有没有在Android上的任何解决方案,以获得BB般的行为如上所述?

Is there any solution on Android to get the BB-like behaviour stated above?

推荐答案

在2010年5月谷歌发布了新的IO会议要求的开发Android REST客户端应用程序这也解释了如何实现的正是我提出的要求。

In May 2010 Google issued a new IO session called Developing Android REST client applications which explains how to achieve exactly what I asked for.

原来,这个问题比较复杂,所以没有方便快捷的解决方案。该解决方案需要Android平台/ API的深入了解。这是由该应用处理/活动的生命周期的灵活性的价格。

It turned out the question is rather complicated, so there is no easy and quick solution. The solution requires deep knowledge of Android platform/API. This is the price caused by the flexibility of the app process/Activity lifecycles.

我觉得为什么这个信息不是来自安卓的第一个版本psented $ P $有些古怪。看起来谷歌知道如何编写100%bugless应用程序和一些营销原因,不同意的做法。试想一下,有多少车的应用程序是由2010年5月。

I feel some oddity on why this info was not presented from the very first version of Android. Looks like Google knew how to write 100% bugless apps and for some marketing reason did not share the approach. Just imagine how many buggy apps was written by May 2010.

无论如何,我很高兴现在我有不便,我们称之为最佳实践方法。

Anyway I'm glad now I have smth we call best practice approach.

这篇关于如何安排在Android长(耗时)的行动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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