Execute long running operations on the service
Asked Answered
A

2

2

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. 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.

  2. 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.

  3. 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.

thanks.

Adversity answered 7/2, 2013 at 20:36 Comment(3)
Please correct me if i wrong but, AFAIK those classes which you extend from Service normally run in another thread. keeping that in mind. i got 2 process now 1 is the UI thread and 2 is the service. Doesn't cause any problem put other thread(network connection) to my app? Isn't it an overhead for the system?Adversity
all Services executing in UI context. So there isn't overhead for system to put network connection processing to other threadSavdeep
@CommonsWare could you give me a hand?Adversity
S
1

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.

Skulk answered 21/3, 2013 at 22:13 Comment(4)
Thanks. @Arhimed you mean that i can use a normal IntentService and AlarmManager to check out on the cloud(RESTws) for any particular message from another remote user?. Doesn't the intentService get killed by the OS once the activity has gone?Adversity
IntentService and Activity run independently, so OS will not kill service if activity is not visible, or even if it is not instantiated.Skulk
Where should be placed the AlarmManager inside of a LocalService or In one Activity. any example?Adversity
As a staring point - #8321943 Here on SO there are a lot of AlarmManager usage examples, please use search or ask a separate question on that.Skulk
S
3

Android Service executing in UI thread. So you should use AsyncTask or another way to work with threads for network requests.

Savdeep answered 7/2, 2013 at 21:13 Comment(1)
you really got the reason. but i'll change the question. because is not what i'm looking for.Adversity
S
1

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.

Skulk answered 21/3, 2013 at 22:13 Comment(4)
Thanks. @Arhimed you mean that i can use a normal IntentService and AlarmManager to check out on the cloud(RESTws) for any particular message from another remote user?. Doesn't the intentService get killed by the OS once the activity has gone?Adversity
IntentService and Activity run independently, so OS will not kill service if activity is not visible, or even if it is not instantiated.Skulk
Where should be placed the AlarmManager inside of a LocalService or In one Activity. any example?Adversity
As a staring point - #8321943 Here on SO there are a lot of AlarmManager usage examples, please use search or ask a separate question on that.Skulk

© 2022 - 2024 — McMap. All rights reserved.