I am trying to make a simple timer application. I want the timer to run even after the user has navigated away from it, but when I run the application and try to come back to it restarts and the timer is no longer running if I do not come back to it in two minutes. Is there anyway to ensure that it will keep running forever without having to come back to the application every two minutes. If need by I can provide sample code or more explanation. Thank you.
Service to work the same way as this.. please check out the underlying post from me. Activities and services using handlers
make sure that the service has the timer and calls a function like this. installed in the service so that the system can be have a look at the service and other examples this gives you a pretty goood example of the how the service works how the service binds,,,, and the functionality of the timer and implementation of it and so on.
Think of creating the Service to solve your problem
Related
Search engines and Android developer website didn't help and I guess you can help with my problem.
I want to make an app for personal use, which is supposed to run all the time on my old tablet (powered all the time). The app will have several features requiring user interaction but independent of those, it should run a background job to check something continuously (real time!) for instance sound detection. It should also always try to connect another device on the network.
That means that job needs to run almost eternally without being killed. Some comments I have found suggested AlarmManager or BroadcastReceiver. But those are triggered by very defined triggers (either time or broadcast). I don't want that, because it should perform its task continuously all the time. This background job should also be able to communicate with the main Activity of my app to report what it is doing and allow user to interact with it (change settings of the job for instance).
Do you know any way how to accomplish this? Is IntentService correct choice for this (hoping that it won't get killed or maybe I should let the Activity to restart it?)
Thanks!
Do you know any way how to accomplish this?
Build your own custom ROM, with a modified version of Android that contains your code as a native Linux daemon.
Otherwise, what you want is technically impossible.
You can come fairly close by using a foreground Service (not an IntentService) and returning START_STICKY or START_REDELIVER_INTENT from onStartCommand(). Android may terminate your process from time to time, but it should restart your service automatically after a short while. That service can use its own background threads to do whatever it is that you are trying to do.
I am using a timer in my application for repeated task. By default, the timer should repeat the task with a delay of one second. I am starting the timer when the application Starts. All this is works perfectly. Also,When I came out of my application and open some other application, the timer keeps running.
When I open Google Maps application, my timer stops running. I don't know why this is happening. I googled and found from the activity life cycle that, if other applications needs memory, all processes will be killed. This is what happening in my case, I guess.
I do not want my timer to stop. It must run always till the user uninstall my application.
Why the above problem occurs?
How to achieve my requirement?
Does Using services will solve the problem? If so, Is there any way to keep timer always ON without using services?
Does Alarm Manager be helpful? Even if the user restarts the phone, the timer should work properly.
Yes, a service will solve your problem. The persistence of an Activity is governed by its lifecycle, so in the end, you have no control of its execution. A service is persistent in that it will not be shut down by the system. If you are doing extensive calculation, however, be warned that the OS will not be as generous with resource allocation as it is with an on-screen activity.
In short:
1) Well, from the information you've given, I suppose you drew the right conclusions.
2) Through a service.
3) Yes, it will solve your problem, no, I don't think there is another way to do that, at least not from within an activity.
4) If you're not actually asking about the persistence of a Java.util.Timer but for a way to have a piece of code executed at certain times, this might be the better (/easier) solution. I don't know how well it works for rather frequent timings, though. A service can be resumed on system restart, so no worries about phone shutdown. You can subscribe to the startup event.
I appreciate any advice about what can I do, because I was searching for an example or a clear explanation and I could not find.
Actually I think that I have to separate problems:
1- I want to start a service from an activity, this service should run in the background in a new thread and keep on running all the time even if I close the activity interface, even if the mobile is not active...
2- Once I open again the activity (by clicking on the application icon), I want that Activity indicate for me that the service is running and I can stop it if I want or I can keep it running and close the activity interface again.
Any relevant tutorials, links or codes are welcome :-)
Thank you, R.
is the Asynk Task not able to do this?? Have you tried it?? Like Java we have a special kind of thread or service in android which does its work in background without affecting your UI means you will not get any ANR(Application Not Responding Errors).
I am creating a app that would fetch data from a website every 2 minutes ..
I need to know whether I can go for a service ?
I tried using handlers and threads but it stops after 3 or 4 hrs I already tried using Async task but it's not working out..
You have to use service because its always runs in background . Thread and AsyncTask and any other handlers not give guarantees to runs in background life time of applicatin.
Here you can find good tutorial for service.
http://marakana.com/forums/android/examples/60.html
http://saigeethamn.blogspot.com/2009/09/android-developer-tutorial-for_04.html
http://mylifewithandroid.blogspot.com/2008/02/double-life-of-service.html
u use the service good option.because service is always running background process
if want know about the service
http://developer.android.com/reference/android/app/Service.html
Best to use a service, actually the best option there is to it. Always consider the users of your application as they do not want to see all that incoming and outgoing flow of data.
I'm trying to find a way to implement this.
basically it's a timer say like in farm ville or such games that count even when you exit the app.
how do I implement that?
Put all logic that's supposed to work even if your app is not visible inside service. Use AlarmManager, Timer there. Service has the same priority as visible Activity, so it won't be killed.
You are going to need to keep a service running to keep track of these things when your app is not visible in the Activity stack. See here:
http://developer.android.com/reference/android/app/Service.html