Android Snooze with Finish - java

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.

Related

Automatic log out for whole application

I have an application which has many activities that logins towards a web service. However my client want this application to log out after a certain timing of inactivity. Which includes screen off or no user interaction. I used the timer before but it will only work on a single activity and if i were to set the timer for 2 activities, 2 timers will be started and i'm not able to reset the previous timer when it moves on to the next activity.
I've thought that if i create a java class and extends Activity and all activities shares the same timer. Which means each time I need to start a timer, I will call the method on the java class.
Please advise or give suggestions on what actually should be done

Android activity LifeCycle, whitch method to run while loop when app is runnig?

I have created an app that is searhcing for bigger and bigger primes and saves them in a textfile. Right know im letting the user to click a button to make the app search for bigger primes and save the to file.
Istead of clicking i want the app to run a while loop in the background and do the searching and saving by it self without any user interaction.
Is the onStart(); a good method to put the while loop in so it runs in the background while the app is running?
Also should i use te Runnable interface to dynamically show the user wich prime is found and saved to the file?
Thank you in advance!
You can use Service if your while loop will do a lot of work or consider using AsyncTask (a few seconds at the most.).
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 (here)
AsyncTask enables proper and easy use of the UI thread.here
As mentioned #EpicPandaForce, if you're doing a CPU intensive(mp3 for eg.) consider using an IntentService because it is executed on another thread.

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).

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 to use Androids location package in a service separate from the User Interface thread?

I have a small Android app that I have been working on that logs GPS data to my SD card in a GPX file. I currently have a main Activity that starts a Service to do all the background work. The service is kept in the foreground in the notification bar to make it the least likely thing to be killed by the OS. Currently I am requesting location updates from the service at the maximum frequency to get the most accurate route. The problem I am having is my User Interface is acting slow/strange. Correct me if I am wrong, but what I have concluded is that I have too much going on in the main thread of the app. My next thought is to try and move the service performing the acquiring and logging of data to a separate thread. I am new to Java/Android so the whole topic of interacting with separate threads is hard for me to wrap my head around. Initially in research I came across IntentServices, which are supposed to make threading easier, but from what I read these don’t seem to mix well with the Android location package because they don’t run long enough. I feel like I am running in circles with internet searches on this topic. I desperately need some guidance on how to achieve the following features for my programs service:
Separate thread from Main Thread
Fetching and storing of data must be the least likely thing to be killed by the OS and run indefinitely once started (don’t worry about battery I will have the device plugged in to power while running the app)
Eventually I will need the ability to interact with the User Interface
Thanks for any help you can offer!
this is a common problem that i have accomplished a lot on
in the launcher or main() ( what Android is calling an Activity ) you do as little as possible ( which amounts to saving the the refs they give you and maybe setting a few other things as long as you are there ) and do ^not^ drop in to a long-running activity
A Service is exactly what you need but instead of trying to pump it into a "hold on to it" state what you do is implement checks for nulls and handle as needed -- trying to "fix" a machine to make it run the way you want here actually involves rescinding you hold on the main thread and letting it go as fast as consistent with the Applicaton's general constraints.
To do this you can simply write a Service - reading everything available - then extend that service and implement Runnable then you run the constructor on that code from the Activity Constructor and do new Thead(yourClass).start(); in the onCreate() checking for Thread.isRunning() before starting it again ...
Service will have an onCompletion() call in it somewhere - it will go through an interface
All this is done in Android in something like start activity for result then you just to the UI stuff in that call or sorta figure out a way for the GUI to get called somehow at some time then check to see if Service is done an so report in the gui

Categories

Resources