Android kills my Threads/AsyncTasks - java

I run my asynctask class of activity, then finish the activity, and asynctask still works. Android after some time killing my asynctask. I tried to add to the function run "Process.setThreadPriority (Process.THREAD_PRIORITY_FOREGROUND);" or "Process.setThreadPriority (Process.THREAD_PRIORITY_BACKGROUND);" but asynctask is still killed.
Yes I know they are Services, but I want the task was completed after the work done, and do not work all the time in the background as Services.

You should use IntentService. This is different from a regular Service. IntentService will result in the service closes it self when it is done. IntentService is not running on the UI thread so you don't need to "handle" it as you need to do with a service..
You start it, it does some work and when it finishes it closes itself, exactly what you need.
There is no way for you to insure that the Asynctask will finish, since it's not build for that purpose.
Also, a service is not "running" as in taking cpu when it has nothing to do even if it's open.

Related

Which concurrency construct to use for running multiple background tasks when the app is paused for Android?

I'm trying to implement multiple tasks in my app that are required to continue running even when the app is paused (when the app loses focus but the user has not completely closed the app).
The first task consists of establishing a connection to a server and receiving updates.
The second task consists of communicating back and forth with a paired Bluetooth device.
Both tasks will not only be ran when the app is paused, they will be required to be run when the app is in focus. When the app is in focus they will be required to update the GUI based on the information they receive. Also both tasks task doesn't really have a set finishing condition except for when the user decides to stop the tasks themselves or when the app is completely closed thus they must be able to be cancelled in mid process. These two tasks will very likely be running for a long time.
In summary:
Two background tasks that can be run when app is not in focus.
Both tasks are required to be able to update the GUI when the app is in focus.
Both tasks must be able to be terminated when the user decides to stop the task inside the app or when the app is completely closed.
I believe that I need to using either a Service, IntentService or AsyncTask.
From my reading I believe that I should be using an AsyncTask but I want to be sure before starting.
Service: Can't manipulate GUI.
IntentService: Can't run tasks in parallel, they will be queued on the same thread.
AsyncTask: Seems fine!
Is this correct or should I be using a combination of things somehow?
You misunderstand the point of Service and IntentService. They are not a concurrency mechanism. They will not run automatically in the background. They run on the same thread as your app. If you need concurrency, you need either AsyncTask or Thread. Please note that if you use AsyncTask to get true concurrency between multiple tasks you still need to use executeOnExecutor().
What Services do is persist even when your Activity is gone, or when it isn't in the foreground anymore (either another activity in your app is, or another app is). So if you need your secondary thread to be running after the Activity is finished, you should have it owned in a Service. If you don't, owning it in the Activity is fine.
An IntentService is basically a Service that does more or less fixed length jobs 1 at a time. There's uses for them, but if they need to do network IO or anything like that they still have to spin off AsyncTasks, because they still are run on the main thread.
You should use a service, the purpose of a service is to have a much lesser restricted lifecycle that can run while the app isn't in the foreground. You can use a BroadcastReceiver or callback/AIDL to send messages back to the calling activity that will manipulate the ui.

Is it possible to have a count down timer continue running even after the user has closed the app

Im thinking of having an android app that has a count down timer. However, i need this timer to be running too even if the user kills the app and once the count down timer reaches 00:00:00, it triggers an action regardless of the application being active. Is there a possible solution to that?
Yes it is possible. Just run on background thread so your thread will continue to run even if app is closed.
Using service is a good idea but you have to work in background thread even in service. Because services work in foreground thread by default.
In your case I think the best way to do that is using AlarmManager. Try it.
In Both case (service and alarm) reboot will kill your thread. However you can recreate it. You can use services that will start again if they get killed. Also you can set a BroadCastReceiver that will be called after reboot is complete. So you can recreate your timer in onReceive method.
*
Yes it is possible. use a service , your service will continue to run even if app is closed.
you should call a service using AlarmManager with in a fix time .
*
using services will be the best option for your requirement. OnFinish of the timer, you can trigger your action.

run a thread behind activities in android

Im developing for an android aplication and I need to run a thread in the background that is going to be check the current gps and show an activity depending of the current location data but the application have diferents activities, and I need this thread running all the time no matter if I change between an activity and other.
Can I do that ?
I know that I can add threads to an activity but I dont know how have a main thread in the background
You need to use a Service which is not tied to any particular Activity. You can read all about Services here
In your case I ssuggest you take a look at IntentService which automatically generates a background thread for you
You want to use a service here:
http://developer.android.com/reference/android/app/Service.html
You can run things in a background thread if you want in the service. And then you can bind to it within your activites to communicate between the service and activities.

How long does the eternal worker thread runs in Android?

hi I have created a thread on the main thread in android which runs for ever to perform some back end operations. so how long does this thread runs? it has been created in "onCreate" method in the activity. so does android kills such threads automatically after some time or does it run until we kill them manually? or it gets killed automatically when we switch to another app?
Hope you know android Activity life cycle. Android ends the process which stays unused for long time. Yes it will be killed after some time if it is in the background.
If an user navigates out ofyour app, the Activity instances in your app chages to different states in their lifecycle. If you start another activity or switch to another app, android calls another lifecycle method namely onPause on your activity as it moves into the background.
If the activity stays hidden for sometime it is automatically ended. You don't need to manually end it. Isn't it fantastic about android?
hi I have created a thread on the main thread in android which runs for ever to perform some back end operations. so how long does this thread runs?
Nothing runs forever. Not even a Service. Anything that runs for a sufficiently long time will likely be killed by Android.
Use an AlarmManager and schedule your task using an IntentService and a BroadcastReceiver. The AlarmManager will invoke the Service every X seconds / minutes.
Take a look at this related to question on scheduling recurring tasks on Android - Scheduling recurring task in Android

How can I make my app run as a background process

I want to make my app run in the background like a process that runs always.
I need it because I want to get locations update for GPS every 2 minutes (longitude, latitude) and to use the information in a method.
For that I need for the app to be running when the phone is asleep or not in the UI of the app in other words I need the app will run always.
I'm sure that there is a way to make it , thanks anyway for any answers :)
This was just the first google search result I found:
http://www.vogella.com/articles/AndroidServices/article.html
The answer here is to use a service, if this tutorial is lacking there are 6.4 billion others.
We have something like this, but it is made up of several parts.
Firstly you will want your code to run (and be registered in the manifest) as a Service
You will probably also want to request android.permission.RECEIVE_BOOT_COMPLETED so that you can write and register a BroadcastReceiver that gets notified by android.intent.action.BOOT_COMPLETED action and its onReceive method kicks off the service.
In our case we also have a front-end activity which also pokes the service to make sure it is running, but it's been a while snce I checked to see if this was still required.
Our service is nearly empty and onCreate immediately calls a custom Handler which then manages the 'ticks' which wakes the Handler and fires a Runnable if there is work to do, but this is where my code diverges from yours. In our case we only attempt to update the GPS location when the service 'ticks' (usually every minute) and there is work to do. It usually only performs a couple of dozen operations per client per day so I can't really advise on how it will impact battery usage.

Categories

Resources