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?
-
1use a service for your purpose – Raghunandan Apr 06 '13 at 17:02
-
does service remain until we destroy it manually? – Muralidhar Yaragalla Apr 06 '13 at 18:10
-
yes it does. It runs in the background. Once operation is finished you need to destroy the service. – Raghunandan Apr 06 '13 at 18:15
2 Answers
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?

- 3,729
- 10
- 57
- 79
-
ya i know the activity life cycle. I think when it moves to "stop" it gets destroyed. so all the threads spawned from the main thread gets killed at that point of time. is it? – Muralidhar Yaragalla Apr 06 '13 at 18:14
-
@muralidhar: You got it wrong there. http://developer.android.com/reference/android/app/Activity.html visit this link again you will find that the activity is destroyed not only in `onStop` but also killed in `onPause` android solely decides when which app is need to be destroyed. By the way your problem is different you wanted to create a service which will monitor continuously. Then in my opinion your question should be different. You can open a new question on that topic. Hope this question is clarified. – NewUser Apr 07 '13 at 02:54
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

- 1
- 1

- 11,095
- 2
- 38
- 49
-
I need to moniter the mobile continuously. so how do i do that? – Muralidhar Yaragalla Apr 06 '13 at 18:35
-
Like I said, schedule a task every X seconds. X can be as low as 15 seconds. Does that fall under your definition of continuous ? – Deepak Bala Apr 06 '13 at 18:38
-
Actually i need to monitor the calls on android. The incoming and outgoing calls, their numbers and duration of call. so what is the best way? – Muralidhar Yaragalla Apr 06 '13 at 19:21
-
Log them to a database and sync the information with your server every X minutes. You **cannot** put this code into an `Activity` / `Service` and actually expect it to work in the long run. It wont. – Deepak Bala Apr 07 '13 at 04:29
-
hi to log the info into the database some code must be running continuously to capture the call info and to log them to database right? so how do i make some code run continuously on android? or is there any different approach? – Muralidhar Yaragalla Apr 07 '13 at 15:31
-
Use `BroadcastReceiver`s to capture the incoming and outgoing calls. Your code need not run continuously. – Deepak Bala Apr 07 '13 at 15:32