4

I want to see for user inactivity in my android app.If user does not perform any activity say for 1 min then the app should go offscreen meaning it should display a dialogbox asking for the password(previously stored in sharedpreferences).If password matches the activity should start up again.Can some body please help me out in this I dont understand from were to start or what to search for.Or can some one provide me some links do achieve this?

Joyson
  • 1,643
  • 5
  • 28
  • 48

5 Answers5

2

During my Serach I found a lot of answers but this is the best answer I got. But limitation of this code is that it works only for activity not for whole application. Take this as a reference.

myHandler = new Handler();
myRunnable = new Runnable() {
    @Override
    public void run() {
        //task to do if user is inactive

    }
};
@Override
public void onUserInteraction() {
    super.onUserInteraction();
    myHandler.removeCallbacks(myRunnable);
    myHandler.postDelayed(serviceRunnable, /*time in milliseconds for user inactivity*/);
}

for e.g you used 8000, the task will be done after 8 seconds of user inactivity.

Community
  • 1
  • 1
A_rmas
  • 784
  • 2
  • 10
  • 26
1

Use BroadcastReceiver with Intent.ACTION_SCREEN_OFF to identify user inactivity in the app. You can use Intent.ACTION_SCREEN_ON to handle screen on situation.

Aju
  • 4,597
  • 7
  • 35
  • 58
  • I need to Display a alertdialog asking for password if user perform no action for some time how can i identify that from Intent.ACTION_SCREEN_OFF. – Joyson Mar 25 '13 at 14:01
  • 1
    This is for Screen off condition. I guess you are not looking for screen off condition....Please check this link.http://stackoverflow.com/questions/4208730/how-to-detect-user-inactivity-in-android – Aju Mar 26 '13 at 05:18
  • check this also..http://stackoverflow.com/questions/4075180/application-idle-time – Aju Mar 26 '13 at 05:19
1

I think this can help you

public void onUserInteraction ()

Added in API level 3 Called whenever a key, touch, or trackball event is dispatched to the activity. Implement this method if you wish to know that the user has interacted with the device in some way while your activity is running.

http://developer.android.com/reference/android/app/Activity.html#onUserInteraction()

Vivart
  • 14,900
  • 6
  • 36
  • 74
1
private CountDownTimer mCountDown = new CountDownTimer(your desire time here, same as first param)
{

    @Override
    public void onTick(long millisUntilFinished)
    {

    }


    @Override
    public void onFinish()
    {
        //show your dialog here
    }
};  


@Override
protected void onResume()
{
    super.onResume();

    mCountDown.start();
}  
@Override
protected void onPause()
{
    super.onPause();

    mCountDown.cancel();
}  
@Override
public void onUserInteraction()
{
    super.onUserInteraction();

    // user interact cancel the timer and restart to countdown to next interaction
    mCountDown.cancel();
    mCountDown.start();
}  

With the above code, all user interaction will be captured. When the user press HOME or SEARCH to leave your application, when they come back what do you want to do is another story. Also, when a phone coming in onUserInteraction would not be called, so if you want to show the dialog after the user coming back from the call and time has expired, then it get more complicated. You have to override onKeyDown and setup a flag just to know when your app pause because of a phone call coming in.

Hoan Nguyen
  • 18,033
  • 3
  • 50
  • 54
1

In your BaseActivity, override dispatchTouchEvent and return false.

long lastTimeStamp;
@Override
public boolean dispatchTouchEvent (MotionEvent ev) {
  if(lastTimeStamp + 5*60*1000 < System.getCurrentTimeMilis()) {
     //your lasttimestamp was 5 mins ago. Expire user session.
    }
   lastTimeStamp = System.getCurrentTimeMilis();
   return false; // return false to indicate that the event hasn't been handled yet
}
Nishant Shah
  • 2,022
  • 22
  • 20