AlarmManager - When to setRepeating and how to get time remaining - android

I have an app which will give the user a bonus power up every 45 minutes. I managed to set a repeating alarm to do it but I have some questions:
1 - Where should I call setRepeating() method? - Having in mind that the alarm should be set automatically and NOT by an onClick event, for instance, I find it tricky to know where to set it. Now I am calling it on the onCreate method of my MainMenu activity. The problem is that everytime he user enters the app it is called. I thought about setting a SharedPreference variable to check if I already called it but it doesn't seem the right approach.
2 - How to the display the time left until next power up? - I would like to display to the user how much time is left until the next power up. The problem is that if the user closes the app and open it again I no longer have a reference to the alarm but it is still running. How can I do that?
Any help will be appreciated. Thanks in advance

I'd use shared preferences for both. When you set the alarm, write a shared preference that says when the next alarm is scheduled for. In onCreate, look at the alarm. If its in the future, no need to set it. If its in the past, set it again. The same answer solves problem 2- subtract the current time from the time in the shared preference, and that's the time left. When the alarm goes off, update the shared preference to the next time.

Related

Take action for every two minutes of inactivity (no user interaction)

I am trying to write to a file whenever the user has not interacted with the application for 2 minutes. Currently am having base activity in which I have overriden the onUserInteraction method. In this method the float time variable is reset and onResume I subtract the time with the current time to check if two minutes have passed. This works fine but sometimes acts crazy. Second approach was using the postDelayed method of the Handler and start a thread. This works perfectly but does not include the case when the app goes to background or the device goes to sleep.Is there a way to cover all these cases. ahve researched a lot. Also came across Wakeful Intent service but read that it is expensive.
Is there a way to cover all these cases.
Yes. ... and it is expensive. Waking the phone up, every 2 minutes is going to drain the battery like crazy.
That said, the answer to your question is that you need to use the AlarmManager, probably in concert with either the WakefulIntentService or the WakefulBroadcastReceiver. Create a PendingIntent and schedule it for delivery every 2 minutes.
I don't know a way of tracking inactivity but there is a way to track user activity. You can catch a callback called onUserInteraction() in your activities that is called every time the user does any interaction with the application. I'd suggest doing something like this:
#Override
public void onUserInteraction(){
MyTimerClass.getInstance().resetTimer();
}
If your app contains several activities, why not put this method in an abstract super class (extending Activity) and then have all you activities extending it.

Widget update by AlarmManager, onUpdate() keeps being called

I've followed the tutorial on widget at http://developer.android.com/guide/topics/appwidgets/index.html, and I also know how to create an AlarmManager to update the widget. What I'm wondering is how to effectively turn off the default onUpdate(). According to the guide lines (at that link), when I use AlarmManager I should set updatePeriodMillis to zero. But onUpdate() keeps being called (because 0 always lapses). If I set it to, say, 30 mins, it updates at that period. So practically it keeps using the value in updatePeriodMillis , which nullifies the effect of using AlarmManager. Am I missing anything?
Update: actually there's another code that triggers the update on another event, so never mind.
Cheers.

What does System.nanoTime() return in Android?

I have a bunch of activities tied together, one into the next and so on. Now during one activity I want to measure elapsed time. As I understand, I would use System.nanoTime() to find the start time, the user does some things, then use it once more to find the end time, subtract the two and voila my elapsed time spent on the activity. But suppose something happens while my activity is running: I already have created the start time, but now the user gets a phone call or something, my activity is put into the background. The phone call ends and the user returns to the activity. Was the timer running the whole time, even while the app was in the background? Is the timer 'reset' since I left the app then came back to it?
Also, when I do initiate System.nanoTime() is it returning the time since the start of that particular activity or the main activity?
EDIT: Suppose I set the first tickmark at a certain point, then the app goes into the background, then it returns to the foreground and I set the second tickmark. Ideally I want the time elapsed along with the time spent in the background; does System.nanoTime() achieve this?
static long nanoTime():
Returns the current timestamp of the most precise timer available on the local system.
You aren't using a "Timer" (that is, a stateful object of any kind) to represent the elapsed time, you are just hanging on to a long. As you pointed out, you will call System.nanoTime() again at some future point and subtract to find the elapsed time.
If you want to exclude time spent outside of this activity, like the example in your question, you will need to use onPause() and onResume() to help you manage the calculations. Or, if you switch to some kind of timer object, use these methods to pause and resume the timer.
You can "start" your "timer" wherever you think makes the most sense. If it's when the user initiates some action (like a button press), od it in an OnClickListener. If it's just to measure how long some method/code path runs, do it at the beginning of that.
according to the doc
System.nanoTime() returns the current value of the most precise
available system timer, in nanoseconds.
So it is not an timer. It just returns the system time in nano seconds. It has not relation with activity.
If you want to measure the lifetime of activity then get the time in onCreate and onDestroy. And if you want the time to know how much time the activity was in foreground then get the time in onResume and onPause.
You will need to override onPause() and onResume() methods in the Activity class so you can pause your timer in pause. and resume in onResume.
You should put System.nanoTime() on onResume()

How to start Login Activity if user wasn't active for 5 minutes:

I need to start the log in Activity if the user wasn't active for 5 minutes in the application
,without considering from what activity he left the application. (by not active for 5 minutes I mean that the user didn't commit any action to the server side)
I have a Date variable inside my Application class:
private Date timeOfLogin;
that's is saved when the user commits log in, in some point of usage the user can get a phone call or
a mail and will leave the application. now this can happen on any screen of the application. And now when he turns on the application again /return to it after finishing his phone call I need to show the log in screen again and not his last activity if 5 or more minutes have passed.
How can it be done? do I have to override every onResume of each activity I have in the application and start the log in Activity if the difference between the timeOfLogin and current date is bigger then 5 minutes? or is there a better way to do that?
Any help would be appreciated.
Thanks.
Have an activity that all of your other activities extend and put the logic in that activity's onResume. (You'll probably find this practice to be useful in a lot of other ways too)
Create a CountDownTimer with 5 minute count down in your ResponseHandler class in the onFinish() show the login dialog. If there is any response cancel the timer and start again.

How to know that last activity of certain type X is closed?

I would like to run some action when all activities of certain type X are closed. The first idea is to have some global counter, that is decremented each time activity X is closed, and once the counter is 0, run some action. Assuming that process can be killed, the counter have to be persisted. But one more issue remains - imagine that activity X can crash (due to some bug in code), and in this case I'll not decrement the counter.
Any idea how to implement it in robust way?
I am not sure what you mean by 'closed', but you should be able to use the Activity Lifecycle callbacks to accomplish what you are asking.
There is a diagram on the following page that shows how the Activity Lifecycle is implemented:
http://developer.android.com/reference/android/app/Activity.html
As you said, you can have a global counter for this.
Maybe the good place for it is in the Application class?
Or u just save you counter in a file and reset it every time you start your application (in a very first activity).
You can also try to save it in shared preferences.
But there is still the problem with crashes. I have just an idea. There is ACRA-library for crash-logging. Take a look in it, how do they catch the crashes.
So you can just write a method for decrementing your counter. And call it from onDestroy() or if a crash occurs.

Categories

Resources