1

So we're doing a project of creating an android app and we're done but since we have some time, I wanted to implement a few extra things. One thing I want to implement but I can't seem to find anywhere is how do I make the app return to the main activity after sitting idle for a certain amount of time. The time, I'm planning on allowing the user to be able to choose that from the settings but I want to find out how to get this feature working first before anything else. All help appreciated.

MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
user2989280
  • 117
  • 3
  • 8

3 Answers3

9

You can achieve this using handler as shown here.

private Handler handler;
private Runnable runnable;

//call in onCreate
setAppIdleTimeout()

private void setAppIdleTimeout() {

    handler = new Handler();
    runnable = new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    // Navigate to main activity
                }
            });
        }
    };
    handler.postDelayed(runnable, timeoutValue * 1000);
}

//reset timer on user interaction and in onResume
public void resetAppIdleTimeout() {
    handler.removeCallbacks(runnable);
    handler.postDelayed(runnable, timeoutValue * 1000);
}

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    resetAppIdleTimeout();
}

@Override
public void onUserInteraction() {
    // TODO Auto-generated method stub
    Log.i(TAG, "interacted");
    resetAppIdleTimeout();
    super.onUserInteraction();
}

@Override
public void onDestroy() {
    // TODO Auto-generated method stub
    handler.removeCallbacks(runnable);
    super.onDestroy();
}
user2989280
  • 117
  • 3
  • 8
MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
  • You can also use AsyncTask, though not really needed in this case – A Nice Guy Nov 24 '14 at 05:00
  • @Arnab in that AsyncTask will you call Thread.sleep(miliSeconds) ? – SilentKiller Nov 24 '14 at 05:06
  • I've used the above code but for some reason, even though there's user interaction, it still times out after the time. I created a global int of 60 for timeout value and exactly after a minute, it returns back to the main activity. – user2989280 Nov 24 '14 at 05:40
  • Same code is working fine for me. Debug if your onUserInteraction is getting called on touch @user2989280 – MysticMagicϡ Nov 24 '14 at 05:46
1

you can try with the following ways :

-> Broadcast for ScreenLight ON and OFF

 BroadcastReceiver myScreenLightBroadcast = new BroadcastReceiver() {

    // This will fire whenever Screen Light will be ON or OFF
    @Override
    public void onReceive(Context context, Intent intent) {

            if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
                // Screen Light is On so you can close your all Activity Except your MainActivity 
                // Or you can move user to MainActivity by starting a NewActivity or Restarting Application
            } else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
                // this will call whenever your Screen will OFF
            }

    }
};
SilentKiller
  • 6,944
  • 6
  • 40
  • 75
0

1 ] Add screen timeout in your settings.

2 ] Use a BroadcastReceiver to trigger the Screen ON & OFF situations.You will get reference from here

Community
  • 1
  • 1
Don Chakkappan
  • 7,397
  • 5
  • 44
  • 59