Android Application: Run code on a timer from the background - java

hello everyone and thank you in advance. My problem is that I have an app that the user will start in the foreground and will set a variable as a timer. The user will most likely close that app, or the phone will go to sleep. So I need the app to background and then run code to send a notification when that timer hits 0.
For the timer I would use unix time and have the background process check to see if it matched or the current time surpassed when the timer goes off.
I would like to know - A) how to send the app to the background with these instructions
B) how to then foreground the app, or at least be able to send a notification after running some code
Again, thank you very much, let me know if I was not clear or if I missed someone's previous thread.

This post should be a good place to start, it looks like what you want to use is a service.
How to run an android app in background?

Look at extending the Android Handler class to create a custom Handler that creates a notification when the Handler receives a Message that you send it using the method sendMessageAtTime(android.os.Message, long uptimeMillis) or sendMessageDelayed(android.os.Message, long delayMillis). When that timer went off could be specified using the long parameter of either of those methods.
Hope this helped!

Related

Defer timer until SCREEN ON event?

I have a service that runs periodically using a timer to invoke itself, but should not run when the screen is off. When the screen on event is fired, the service should run, but only if it's past when the timer would have fired.
Right now I still run the timer continually, but have the service do nothing if the screen is off. I can also run the service via a broadcast receiver when the screen turns on - but this runs the service every time the screen is turned on, instead of only when it's past when the timer should have run. Recording this state in the service doesn't seem to work as Android will kill the JVM for the app in between executions.
What would be the cleanest/correct way to implement this type of behavior?
So there are a couple of intents that you can listen for, Intent.ACTION_SCREEN_OFF and Intent.ACTION_SCREEN_ON. However, these don't work as manifest receivers, they need to be explicitly registered.
See this answer: https://stackoverflow.com/a/9478013/1306452
In all scenarios, it's going to involve a long running service in order to listen for those events.
One thing to keep in mind is that these intents only listen for when the device becomes non-interactive and vice versa, not specifically when the screen goes off (see the description on the Intents).
The best way for you to achieve this behaviour would be to listen for when these intents with a long lived service, started with START_STICKY to help guarantee that the service is running. The service can register a receiver for the SCREEN_ON and OFF events, and when it gets these events either do nothing if the timer has not elapsed, or continue if it has.
This won't be nice on your battery life, and what ever you are doing it doesn't sound like it's going to be a pleasent user experience. You might also want to step back and see if there's another way around this obstacle (my 2 cents).

What is the reason ScheduledExecutorService is not executing while display is off?

Im fairly new to Android and have encountered a problem, which i would like to understand. The problem is that the java.util.concurrent.ScheduledExecutorService doesn't seem to issue its tasks while the display of my smartphone is off. I was using a java.util.Timer before which didn't have this issue, but was transitioning to ScheduledExecutorService because of the need to wait for the end of the task execution after stoping the Timer. The Timer is used in a Service running in the background.
Note: Using AlarmManager isn't an option, because i want to understand the problem with ScheduleExecutorService or else will use Timer again (synchronizing the shutdown).
Here is how the task is scheduled:
mScheduler = Executors.newSingleThreadScheduledExecutor();
mScheduler.scheduleAtFixedRate(mTask, 1, 1, TimeUnit.SECONDS);
To furher clarify the purpose: I am trying to update a Notification as well as parts of the User Interface periodically, this doesn't need to be done if the device wants to go into "sleep" mode, but i would need to handle this case. I think understanding why ScheduledExecutorService is behavoring the way it does compared to Timer will help me handle it.
UPDATE:
As recommended by Chris Stratton, i'm using a BroadcastReceiver to receive the Intent.ACTION_SCREEN_ON and Intent.ACTION_SCREEN_OFF Intents to pause/resume the ScheduledExecutorService, which works like a charm. However there seems to be another issue with the ScheduledExecutorService while having a phone call, the execution doesn't stop but seems to be throttled somehow. This is only the case if the screen is turned off due to the proximity sensor (no intents are received in this case either)...
When the screen is off, if no other applications are holding WakeLock, the CPU suspends.
You should use a PARTIAL WakeLock whenever you need something done while screen is off.
And make sure you release it as soon as you've done all the ackground job.

Android Snooze with Finish

Hi I am starting to look into android development and I couldn't find a good tutorial on snooze function. Here goes my question:
I have a simple alarm clock that I'd like to implement snooze. I have a AlarmActivity pass an intent to start AlarmAlertActivity. However, I when the snooze button is pressed, I want to call finish on AlarmAlertActivity. I have the snooze method written in AlarmAlertActivity using timer. However, when the AlarmAlertActivity class is finished, the timer no longer runs. I don't really want to do another intent to go back to AlarmActivity, because there might be multiple snoozes. Any help is appreciated!
You want to implement this functionality as a Service, not as part of an Activity. From the docs:
A Service is an application component representing either an application's desire to perform a longer-running operation while not interacting with the user or to supply functionality for other applications to use.
You can design your AlarmAlertActivity to interact with the service (starting it if necessary). The service can then broadcast a message when the snooze goes off.
It's hard to provide any specific advice because you have not provided any details about what you're doing. However, the code on this thread might provide some guidance.

Run AsyncTask every X second to update GPS position

I have a class that extends AsyncTask, which fetches the gps cordinates from the device.
I would like to keep the data updated, so my initial though was to call the class from a timer or a handler. Is this a smart way to implement it, or am i better off listening to the onLocationChanged and do my updates in there?
Hope you get the idea, otherwise ill elaborate.
Thanks!
An alarmManager will be a good solution here.
These allow you to schedule your application to be run at some point in the future.
When an alarm goes off, the Intent that had been registered for it is broadcast by the system, automatically starting the target application if it is not already running.
So when alarm gets triggered, call your execute() method of Async task.
For more info see this: http://developer.android.com/reference/android/app/AlarmManager.html
I also want to implement the same in my app in near future. If you get the solution, don't forget to update the post about how you implemented it.
Thank you.

How do i get service timer for my video recording application?

Hi guys i wanted a "service" timer which enable my camera to show the elapsed(incremental) time of a timer when the video recording process has started in such a format 00:00:00? But i do not know how to code the above function in a service activity since it poses the problem of not allowing xml content to link with my mainActivity which in my case is a video recording activity to update the elapsed time on a textview, correct me if i'm wrong or is there a work around/solution?
And when the camera exits and returns the service should still keep track of the time since started and update the textview accordingly... Can someone help me on this matter i'm rather new to android/java programming?
This is what i have tried out so far for my mainActivity which performs the recording and display the updated elapsed time of the timer..
There may be a better answer, but you could just obtain the time when the recording starts and remember it. It's not clear what owns the textview, but whatever does could update it from the current time by subtracting the start time. You can also pass the service a callback handler which it could use to send you update messages.

Categories

Resources