android 设计考虑:AsyncTask vs Service (IntentService?) [英] android design considerations: AsyncTask vs Service (IntentService?)

查看:19
本文介绍了android 设计考虑:AsyncTask vs Service (IntentService?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设计一个 Android 应用,需要执行以下步骤:

I'm designing an android app which will need to do the following steps:

  1. 用户按下按钮或以其他方式指示同步数据".
  2. 同步过程将使用 REST 网络服务将数据移入和移出服务器.
  3. 数据将本地存储在 sqlite 数据库中.
  4. 同步过程应向 UI 提供状态更新/消息
  5. 不应允许用户在同步过程中徘徊到应用程序的其他部分并做更多的工作.

第一次运行同步过程,可能需要 10-20 分钟.初始同步后,传输和存储的数据较少,我希望这个过程需要 1-2 分钟或更短的时间.

The first time the sync process runs, it may take 10-20 minutes. After the initial sync, less data will be transferred and stored and I expect the process to take 1-2 minutes or less.

我已经阅读了很多关于 android 的 AsyncTask 以及使用服务的各种示例......但我并不完全理解选择一种设计的设计考虑和权衡在另一个之上.我目前使用 AsyncTask 剔除我的演示项目.在观看(大部分)开发 Android REST 客户端应用程序后:http://code.google.com/events/io/2010/sessions/developing-RESTful-android-apps.html# 我对这里描述的设计模式感到很困惑复杂,也许是因为我只是不明白".

I've been doing a lot of reading about android's AsyncTask and various examples of using a Service ... But I don't fully understand the design considerations and trade-offs of choosing one design over the other. I currently have my demo project stubbed out using an AsyncTask. After watching (most of) Developing Android REST client applications: http://code.google.com/events/io/2010/sessions/developing-RESTful-android-apps.html# I'm left confused the design patterns described here feel overly complex, perhaps because I just "don't get it" yet.

我来自 java、spring、web 和桌面应用程序背景.从手持设备的角度思考和设计对我来说是全新的.(当屏幕布局改变时会发生什么?当我运行同步时电话响起会发生什么?)退后两步,如果初始同步将是一个如此长的运行过程,有没有更好的方法我要考虑问题-> 解决方案、用户体验、用户对手机上运行的应用程序的期望?

I come from a java, spring, web and desktop application background. Thinking and designing in terms of a handheld device is quite new to me. (What happens when the screen layout is changed? What happens when the phone rings while I'm running a sync?) Taking 2 steps back, if the initial sync IS going to be such a long running process, is there a better way for me to think about the problem->solution, the user experience, the user expectations of an application running on a phone?

希望听到一些经验丰富的 Android 开发者的意见,他们已经与这些问题搏斗过.

Would love to hear from some more experienced android developers out there who have already wrestled with these questions.

推荐答案

在我看来,这是主流/普通 Android 开发中最棘手/最困难的部分.例如,在 BlackBerry 上,这有时更容易.

In my opinion this is the most tricky/hard part of a mainstream/average Android development. For instance on BlackBerry this is IN TIMES easier.

绝对需要使用Service.

AsyncTask 不适合,因为它通过 Context 句柄与您的 Activity 紧密绑定"(否则您将无法从您的 AsyncTask 更新 Activity 的 UI).但是,一旦 Activity 进入后台,操作系统就可以杀死 Activity.进入后台的一个示例原因可能是来电 - 用户切换到电话应用程序,因此您的 Activity 变得不可见.在这种情况下(取决于当前的 RAM 状态)操作系统可能会决定终止其中一项后台(用户不可见)活动.

AsyncTask does not suit, because it is tightly "bound" to your Activity via a Context handle (otherwise you would not be able to update UI of the Activity from your AsyncTask). However an Activity can be killed by OS once the Activity went in background. An example reason of going to background can be an incoming call - user switches to Phone application so your Activity becomes invisible. In this case (depending on the current RAM state) OS may decide to kill one of the background (invisible to the user) activities.

一些开发人员通过安排一个静态的东西来解决这个问题,以便在里面有一个长期运行的动作.有些人推荐使用 Application 实例.这是因为静态的东西和 Application 存在,而整个应用程序进程存在.然而,这些都是不正确的解决方法.当操作系统决定是时候终止时,Android 中的进程也可能被终止.Android OS 对它可以杀死的内容和顺序有自己的考虑.所有进程都分为 5 个级别的可杀死性".这里是指定这些级别的文档.在那里阅读很有趣:

Some devs workaround this by arranging a static stuff for having a long-running actions inside of. Some recommend to use Application instance. This is because static stuff and Application exist while the whole app process exists. However those are incorrect workarounds. Processes in Android are also may be killed when OS decides it is time to. Android OS have its own considerations about what it can kill and in what order. All processes are devided to 5 levels of "killability". Here is the doc where those levels are specified. It is interesting to read there:

因为运行服务的进程是排名高于有背景的一位活动,发起的活动长时间运行的操作可能会做得很好为该操作启动服务,而不是简单地产生一个线程 -特别是如果操作将可能比活动更持久.例子其中正在播放音乐背景和上传图片由相机拍摄到网站.使用服务保证操作将至少有服务进程"优先级,不管是什么发生在活动中.

Because a process running a service is ranked higher than one with background activities, an activity that initiates a long-running operation might do well to start a service for that operation, rather than simply spawn a thread — particularly if the operation will likely outlast the activity. Examples of this are playing music in the background and uploading a picture taken by the camera to a web site. Using a service guarantees that the operation will have at least "service process" priority, regardless of what happens to the activity.

您的 Activity 在用户启动长时间运行的操作时应显示 ProgressDialog 以确保用户在操作运行时不会执行任何其他操作.该指南位于此处.

Your Activity where users initiate a long-running action should show a ProgressDialog to make sure user does not do anything else while the action is running. The guide is here.

此外,如果您的 Activity 当前不可见,您很可能希望使用 NotificationManager 通知用户您的长时间运行的操作完成(或失败).下面是 NotificationManager info 开始.

Also, you'd most likely want to use the NotificationManager for notifying the user about your long-running action completion (or failure) if your Activity is currently invisible. Here is the NotificationManager info to start from.

这篇关于android 设计考虑:AsyncTask vs Service (IntentService?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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