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
Related
I have to implement a periodic request to the server every 15 minutes to update database info, that should be running async while the user is using the app and if the app is closed or paused it shouldn't keep working until the app is resumed
I'm using WorkManager to do that and is already working but I realized that when I minimize my app, the worker keeps calling doWork() every 15 minutes. Killing the app works well because the doWork() is not called.
So the question is how I can disable my worker when app is minimized? Or what I should use instead of WorkManager?
extend Application class (and register it in manifest)
use registerActivityLifecycleCallbacks and inside registered Application.ActivityLifecycleCallbacks set up some value, e.g. activitiesStarted++ in onStart and activitiesStarted-- in onStop
when your WorkManager fire your code just check at first how many your Activities are started, if ==0 then all are "homed" (or killed)
in HERE you have some samples how to do this properly
there is also some clever lib for your purpose - Lifecycle
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.
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.
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.
My problem is: I need to start countdown timer in my app which will work independently my app is opened or closed ...or even if my smartphone is off.
I really wonder how does android standard clock app works. I mean how it counts time even when my phone is off? Could anybody help me with this? I will appreciate any answer!
Internal to an android device will be a clock which runs from the battery and potentially syncs with network time when it has signal.
You can use AlarmManager and PowerManager to get your code to run at specific times, even if the phone is turned off:
Sometimes, it may be necessary for your android app to complete a task
sometime in the future. In order to do this, you must schedule an
activity (can also be a service) to be run using Android’s
AlarmManager. This post will show: How to set up a receiver for the
scheduled event How to create an activity from this receiver Using the
AlarmManager and the created classes to successfully receive and
process a scheduled event
http://justcallmebrian.com/2010/04/27/using-alarmmanager-to-schedule-activities-on-android/
http://www.techrepublic.com/blog/software-engineer/use-androids-alarmmanager-to-schedule-an-event/