Timer even outside the app - android

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

Related

how to leave my acvitity to continue run when the apps is not on front ?

I wrote some application that write to file the GPS location every 30 seconds.
When the application is not on focus i want to continue to write to the file.
How to do it ?
That's not possible due to the lifecycle of an Activity. As soon as you lose focus or hide it it will go into the corresponding state of onPause, onStop or even onDestroy if its not needed anymore.
What you have to do in order to fix it to start a Service. There are different kind of services and in your case an unbounded background service would do the job.
You possibly want to start it in the onStop() and stop it at onResume methods
See here for more android documentation on services
https://developer.android.com/guide/components/services.html
To get things to continu working after the app is send to the background, you would need to use a Service instead of a Activity.
Check out this link, the Android Developer website about services:
https://developer.android.com/guide/components/services.html
Also read this, about the fundamentals of Android apps:
https://developer.android.com/guide/components/fundamentals.html

Timer stops in the background : Android

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.

android correct control of a service

I have a thread (Updater) inside a service (RefreshTasks) that check if there are some updates on the server. Then I have 4 activities that use those data.
But I have a problem in managing this service...
I would like to keep the service active along the whole application, even if the screen goes off. What is the correct practice to manage a service like this? In this moment I start and stop the service every onResume and onPause method of all activities... but this implies that the service will stop when the screen goes off.
Any suggestion?
Thanks AL.
The Service instance continues to run also when your app is paused.
I suggest you to look also this service provided by Google, which is a good solution for push notification in communication between the device and your service:
https://developers.google.com/android/c2dm/
And this is a good tutorial to start with:
http://blog.mediarain.com/2011/03/simple-google-android-c2dm-tutorial-push-notifications-for-android/

Service or Thread or AsyncTask?

I need an advice for my latest app. It will show the user the latest subtitles released, and it will give him a notification in case new subtitles of his favourite series have been released; what should I use to achieve this?
I was thinking to create and run a service which will include a
timer.scheduleAtFixedRate(new TimerTask() {...
but at the same time I really don't know how to make it interact with my app (if the app is opened I don't need any notification but I need to update the GUI).
I could use a thread but I'd like it to run it even after the main activity has been killed...
or I could use a AsyncTask so it would be easier to deal with the Application GUI.
Which solution should I use? I was thinking I should simply use a service (the first solution), but I'm not too sure about it, and furthermore I don't know if there is any way to make a service communicate with an activity periodically...
Thanks for your help.
A service communicating with an activity is called bound service, that's what you should use IMO.
However, you say that when the activity dies, the service should keep running. But if the service is bound to your activity and the activity is dies, the service dies too.
I suggest you to read here about it.
Check and see if you can bind a service to an activity, and when it dies, unbind and let the service continue to run independently.
If you can't, the activity could unbind itself, then start the service independently (with startService rather than bindService).
if you are showing notifications, why not use C2DM messages for communicating with the app. The only thing would be that there would be popups shown to the user even if your app is not running. No need to use threads/services.

How do I make an application run in the background indefinitely?

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

Categories

Resources