I know I should use Services in Android to do stuff in the background from a non-UI thread.
However, I would like to know what is wrong with just spawning a background thread to do the work from an Activity class (within an onClick event for example).
Thanks in advance.
Actually, Services are used for long running tasks, especially those, that run when your activity is not running. Threads can be used for making some tasks inside your activity. This states, that a Thread, created inside your Activity, can not live outside of the activity that's created it, when a Service can. Hope this helps.
Related
I'm new to Android and Java. I need some clarification about threads and their relationship with activities. I'm thinking for this case: A code running in background that can be visible for two activities. I don't know if this can be achieved using threads. From what I've read, there would be no problem using a service, but I'm not sure in the case of threads, so here my questions: If a thread is running in background, can it be "visible" for two or more activities? For example, the most basic sample about thread changes the text in some activity or update the value for a progress bar but, can this same thread be used to do the same in a second activity? Or I need to create a new thread for the second activity that do the same? Is a Service better approach for this case?
Thank you.
If a thread is running in background, can it be "visible" for two or
more activities?
If I understand "visible" as it can access these Activity instances then yes, why not? It's just another object.
For example, the most basic sample about thread changes the text in
some activity or update the value for a progress bar but, can this
same thread be used to do the same in second activity?
Of course. Just keep in mind that the only thread that can draw UI is the UI thread (also called main or system thread). To do any UI update from any other thread you created, you need to call runOnUiThread().
Is a Service better approach for this case?
If this thread is only for updating your app UI, then a background thread is the best solution. A Service will keep running after your app has closed and also you would need to do IPC to communicate app and service, which is slower and requires more code.
You also might consider using AsyncTask instead, it is more suitable for your case because some of its methods run directly on the UI thread (so you don't need runOnUiThread)
currently, I am using service to work in background and update UI periodically accordingly
That service is fired every 30 seconds
to avoid hanging UI requests....I specified a separate process for it, by adding the following.
android:process=":remote"
But, in that case, my UI stopped getting updated.
Need the resolution for this.
You can register a BroadcastReceiver programmatically in your activity to get data back from the service and update the UI. You may also want to update the UI in your onResume() method in case the activity gets paused for any reason and then brought to front.
Don't forget you must update your views in the UI thread, otherwise an exception will be thrown.
If the service work is only relevant to the UI update, you may consider using a Timer/TimerTask or a Thread in the same activity in order to simplify the process.
EDIT: If you're working with webservices, consider using a single AsyncTask to perform all the work in doInBackground(...), and update in publishProgress(...)
Hope it helps.
I have an Android app which uses a service to continuously update a textview in the view when the app is open. It updates it using a broadcast receiver which it sends intents to. By the nature of the app, the service needs to continue to run even after the app has exited/the view is gone. Obviously it's not wise to run the service on the main thread in this case, as that will hog up too much and eventually force close and crash the app. So what is the best way to keep this service running? An ASyncTask? A separate thread? How would I go about doing this? I tried it with an ASyncTask like this:
private class MyTask extends AsyncTask {
#Override
protected Object doInBackground(Object... objects) {
return getActivity().getApplicationContext().startService(intent);
}
#Override
protected void onCancelled() {
getActivity().getApplicationContext().stopService(intent);
super.onCancelled();
}
}
Oh yeah, I'm using getActivity().getApplicationContext() because this is inside of a fragment.
So this works fine; I call MyTask.execute() when I want to start it and MyTask.cancel(true) when I want to stop it, but I'm pretty sure I'm not using it how it should be used. I don't even know what I'm supposed to use as the argument for the execute method (??). And it doesn't seem to be the best idea to just start a service in it and nothing else, surely that would work better using just a thread? What's the proper way to go about this, in order for it to work the way it was intended to? By the way, although this does work for much longer than running it on the main thread, it does still crash after several hours.
You can use async tasks to do background tasks in android. Thread handling on your own is ill-advised unless in specific circumstances. The async task will run in a background thread even the user switches to another view and you can get callbacks at periodic intervals using onProgress() update too. Here are some good tutorials on async tasks to get you started. Please go through them carefully since async tasks will be help you a lot in android development.
http://mobileorchard.com/android-app-developmentthreading-part-2-async-tasks/
http://www.vogella.com/articles/AndroidPerformance/article.html
here are the official docs:
https://developer.android.com/reference/android/os/AsyncTask.html
Update: I think I figured it out. I was way off in what I thought I needed to do, apparently for the kind of service I had I only needed to run it in the foreground (so using the startForeground() method). It seems to be working now. Sorry!
You should not start service in another thread than event thread, instead if some code needs longer time to execute in the service, put that code in another thread in Service Class only.
To execute Service in some other process than the main application, you should define process tag in manifest file, in the service.
Calling startService on background thred does not make the service run on a background thread.
You should call startService normally, without using Async Task.
In your service you should create a new thread to do the work. Here you should not use AsyncTask because this service can run indefinitely, it is not a defined task.
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
I have a service in my Android application that has a UI its running. The problem I am running into, it that on various devices, it doesn't seem to maintain control of the main thread. For instance, it does not ALWAYS respond to the back button being pressed. Is there a way to ensure that the service always has control of the UI?
Thanks.
Solved it! For anyone that cares, I need to called the startForeground method for the Service.