I have a question about services: What would be better, create a thread within a service, or service within a thread?
The question comes because I am implementing an error reporter for my application, so that when the application fails it sends me a report with what has happened. My idea is a notification when an error occurs from here show activity Activity and creates a thread to retry delivery until it succeeds or reaches a minimum number of attempts.
Remaking the question, how is the system least likely to remove my process (if out of memory): With a thread within a service, or service within a thread?
I think the best is the first choice, but I saw a code in the same android people, here:
(line 640)
code from google to keep alive a thread??
Where they use the second. What do you think about it?
I don't need an AsyncTask, because I don't need to interact with the UI thread, I'm not showing anything at this point to the user.
Create your worker thread inside the service.
Related
For my App. I am using the Background service i.e, Lifecycle that excutes for every 200 ms . Now i want the Thread in Service which checks some condition if the lifecycle is on. Can anyone please suggest me how can i implement this logic.
Simply create a Thread in the Service class with come kind of callback. Services are not some magical, remote components of the app, you can create Threads in them just fine. Or even better, go for an AsyncTask instead of Thread, as it has a callback mechanism already implemented (the onProgressUpdate() method) :-)
I am new to Android application development. My first project is to create a tuner, which requires to record audio and analyse it in real time.
I have read a lot on background operations in Android, but I am having trouble deciding what to use:
Asynctask : Android documentation says it should be kept for short computations, but I need to analyse data for more than a few seconds.
Intentservice : Better suited for long computations, but it can't be stopped whenever I want with a button.
Worker thread : The limitations seem to be similar to that of Asynctask.
On the following link is an example I found that is similar to what I want to do. Can a worker thread still be a good choice for long computations ? Is it thread safe to use a while loop with a flag to stop the computation ?
http://developer.samsung.com/android/technical-docs/Displaying-Sound-Volume-in-Real-Time-While-Recording
Edit: I have successfully created a bound service. Inside this service, a new thread is created to update a value in a loop, which is then broadcast. But when I unbind from the service, the thread continues to run. The value will still be updated even if I close the app and restart it.
So I am back to my initial problem. How is such a thread stopped ?
Edit: Problem solved with a simple flag activated in onUnbind() that stops the loop inside the thread.
You actually want a Service, but not an IntentService. You want a Service that will run in the background and which can be controlled through Activity (and will keep working after Activity is closed). This is a common architecture for such tasks. Here is an example of open source music player for Android:
https://github.com/kreed/vanilla/blob/master/src/org/kreed/vanilla/PlaybackService.java
I have doubts when it comes to Services.
I know the service runs in the background, but I thought you necessarily needed to create a thread within the service, otherwise it would block the main thread and you would get an ANR error.
I thought I got it! But then I read this in the Android Developer guide:
…if your service performs intensive or blocking operations while the user interacts with an activity from the same application, the service will slow down activity performance. To avoid impacting application performance, you should start a new thread inside the service.>
The paragraph mentions "intensive or blocking operations", but doesn’t mention an ANR error, it mentions performance. So how a service works?
Supposing an Activity starts a Service. Does the Service run by default in the background of the main thread? Meaning that you can still use your activity while the service is running, but since your Activity and the Service are sharing the main thread’s resources, it would slow down the performance of your activity, and if Service is doing CPU intensive work, it could leave no resources for the activity to use, and eventually you would get an ANR error.
The best practice (but not necessarily, if the service is doing light work) would be to start a new Thread within the Service, now the Activity and the Service are using their own thread’s resources. Then you could close your activity, but Android keeps the service thread alive.
Is that it? Thanks =)
I thought you necessarily needed to create a thread within the service, otherwise it would block the main thread and you would get an ANR error.
Correct.
The paragraph mentions "intensive or blocking operations", but doesn’t mention an ANR error, it mentions performance.
Feel free to file a bug report at http://b.android.com to get them to improve this portion of the documentation. :-)
Does the Service run by default in the background of the main thread?
The lifecycle methods of a Service, such as onCreate() and onStartCommand(), are called on the main application thread. Some services, like IntentService, will provide you with a background thread (e.g., for onHandleIntent()), but that is specific to that particular subclass of Service.
and eventually you would get an ANR error.
Yes.
The best practice (but not necessarily, if the service is doing light work) would be to start a new Thread within the Service, now the Activity and the Service are using their own thread’s resources. Then you could close your activity, but Android keeps the service thread alive.
Basically, yes. Here, "light work" should be less than a millisecond or so. Also, some things that you might do are naturally asynchronous (e.g., playing a song via MediaPlayer), so it may be that the Service itself does not need its own thread, because something else that it is using is doing the threading.
I am lagging in basic android concept,
As per the documentation Service is running in MainThread. and Activity(UI) also running in same thread. In what way MainThread in the android application is running both components code (Service and Activity) paralelly. How android is handling this as local Service is not a separate process. Please give me detailed explanation or any specific links
You'll notice most, if not all of the "main UI thread" methods you write, are callbacks -- they are not running any single main loop, but rather are called when needed, to perform bried tasks (ie: change UI). There is clearly an android main loop that is listening and trigerring these methods.
That same android main loop sometimes also runs Services and Handler code.
As a result, basic simple Services should not kick off extended work loops, as that would prevent focus getting back to the UI methods.
Finally, if a UI method (or Service or Handler) starts doing a lot of work, the android main loop will trigger an Application Not Responding (ANR) to kill the app.
I am editing the code of an android app that is making GPS calls in a service. LocationListener. It also uses ServiceConnection
In some views the device decides that my application is taking too long to respond, and that the user can either "Force Close" or "Wait". Before this popup appears, the application is still usable by the user, they can scroll, slide, press buttons etc.
I am only assuming this is related to the GPS service as it is running whenever this problem happens.
I heard that this problem has to do with a thread running on the UIthread, instead of a background thread. But I was sure that services run asynchronously in the background thread.
Insight appreciated
Using a service does not necessarily spawn a new thread, the service call runs on it's caller thread. From the android API Service doc at:
"Note that services, like other application objects, run in the main thread of their hosting process..".
You can specify the service to run on a different process but best practice is to spawn a new thread in the service.
More on android service at:
http://developer.android.com/reference/android/app/Service.html
I fully recommend you to extend AsyncTask,it enables proper and easy use of the UI thread. Allows you to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers. You may want to do all the computing in doInBackground method.BTW Force Close or Wait is a classic behavior for this kind of issues.
Good Luck!!!
Processing in a service can still cause your application to hang.
The solution you should be looking at implementing is to run any logic that may bog down your activity in a separate thread. This includes things like: Database updates/insertions, Network communication, and any other pieces of long running code.
The AsyncTask is a convenient method for this as you can manipulate the UI in the onPreExecute and the onPostExecute functions.
You can implement an AsyncTask directly in your service as a subclass.
Hoepfully this helps!
Cheers