0

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.

android_eng
  • 1,370
  • 3
  • 17
  • 40
  • Not an Android developer at all, so I might be missing something, but I doubt "sometimes goes crazy" is a technical term in Android development. Please specify exactly what happens and under which circumstances and what you have done to try to ascertain its cause, or it is unlikely people will be able to help you much. – Amadan Aug 24 '15 at 01:01

2 Answers2

0

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.

jaxarroyo
  • 190
  • 2
  • 15
0

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.

G. Blake Meike
  • 6,615
  • 3
  • 24
  • 40