在服务执行长时间运行的操作 [英] Execute long running operations on the service

查看:139
本文介绍了在服务执行长时间运行的操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要建其中有一个服务的应用程序。我知道,在相同的用户界面的进程中运行的所有应用程序组件,至少你在清单中指定。因此,为了避免ANR的消息,我有三种方式。

I'm building an application which have a Service. I know that all application components run in the same UI process, at least you specify it in the manifest. So to avoid ANR's messages i have three ways.

  1. 在清单在一个单独的进程中运行指定服务,像  机器人:工艺=:远程但我读过一些计算器的职位,
     说,这是不是一个好主意,因为它消耗了大量的电池和CPU处理。  既然这些后我真的很尊重是从受信任的人。

  1. Specify the service in the manifest to run in a separate process like android:process=":remote" but i've read some StackOverflow's post that
    says that it's not a good idea, because it consume a lot of battery and cpu processing. That i really respect since those post are from trusted people.

使用的IntentService。它可能出来一个好办法。但我需要我的服务,如果连运行  活动是不可见的。因为我需要的服务,不断检查对新的Web服务  来自其他用户的消息,并通知直通通知。难道是更多钞票使用  一个IntentService?是一个ellegant的解决方案。

Use an IntentService. it's probably a good way out. but i need my service running even if the activity isn't visible. Because i need the service keep checking against a web service for new messages from other users and notify thru Notification. Could it be posible using a an IntentService? is that an ellegant solution.

使用本地服务。只是删除了android:程序=:远离清单文件的属性。  但我得到了一些...... OnMainThreadException错误。这意味着我需要创建一个特殊的  线程来执行那些长期运行的操作或使用的AsyncTask,

Use a local service. just removing the android:process=":remote" attribute from the manifest file. But i get some ...OnMainThreadException errors. it means that i need to create an special thread to execute those long running operations or use AsyncTask,

也许有另一种方式来做到这一点。请让我知道,如何对服务进行长期运行过程中出现的操作。实在是当务之急。

maybe there are another ways to do it. please let me know, how to execute long runnig operations on the service. is really imperative.

感谢。

推荐答案

首先,让我们接受的是有两部分:积极参与(网络)和下积极参与前一段沉睡的一部分。我想你可以使用一个简单的本地 IntentService 活跃的部分。它的完成每一个活跃的部分应使用 AlarmManager 重新安排下一个活动的一部分。这种方法可以确保在睡眠的部分你的应用程序不消耗资源。你是正确的 - 一旦 IntentService 得到的结果是psented用户可以使用通知$ P $

First of all, let's accept that there 2 parts: an active part (the networking) and some sleeping part before the next active part. I think you can use a plain local IntentService for active parts. Each active part on its completion should reschedule the next active part using AlarmManager. This approach makes sure your app does not consume resources during sleeping parts. You are right - once the IntentService gets a result to be presented to user it can use Notification.

这篇关于在服务执行长时间运行的操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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