Reputation: 253
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.
Upvotes: 2
Views: 4029
Reputation: 253
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!
Upvotes: 1
Reputation: 4389
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.
Upvotes: 0
Reputation: 20553
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
Upvotes: 3
Reputation: 29199
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.
Upvotes: 0