How to close a window / activity after a certain amount of inactivity

I am developing a music player application for Android that will feature pop-up controls. I am currently trying to close these controls after a period of inactivity, but there seems to be no clearly documented method for this. So far, I have managed to combine the following solution using several suggestions both from this site and with others.

private Timer originalTimer = new Timer(); @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.playcontrols); View exitButton = findViewById(R.id.controls_exit_pane); exitButton.setOnClickListener(this); View volUpButton = findViewById(R.id.controls_vol_up); volUpButton.setOnClickListener(this); View playButton = findViewById(R.id.controls_play); playButton.setOnClickListener(this); View volDownButton = findViewById(R.id.controls_vol_down); volDownButton.setOnClickListener(this); musicPlayback(); originalTimer.schedule(closeWindow, 5*1000); //Closes activity after 10 seconds of inactivity } 

And the code that should close the window

 //Closes activity after 10 seconds of inactivity public void onUserInteraction(){ closeWindow.cancel(); //not sure if this is required? originalTimer.cancel(); originalTimer.schedule(closeWindow, 5*1000); } private TimerTask closeWindow = new TimerTask() { @Override public void run() { finish(); } }; 

The above code makes perfect sense for me, but it closes any user interaction. However, it closes normally if it is untouched and will not close after interaction if I delete the second schedule, so this seems to be the problem. Also note that I assume that I will move this time task to another thread in order to maintain a consistent user interface. First I need to get it to work: D. If there is more information I need to provide, please ask and thanks for any help ... You guys are brilliant!

+4
source share
2 answers

Based on the @CommonsWare suggestion, switch to the handler. Works great. Thank you very much!

 private final int delayTime = 3000; private Handler myHandler = new Handler(); @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.playcontrols); View exitButton = findViewById(R.id.controls_exit_pane); exitButton.setOnClickListener(this); View volUpButton = findViewById(R.id.controls_vol_up); volUpButton.setOnClickListener(this); View playButton = findViewById(R.id.controls_play); playButton.setOnClickListener(this); View volDownButton = findViewById(R.id.controls_vol_down); volDownButton.setOnClickListener(this); musicPlayback(); myHandler.postDelayed(closeControls, delayTime); } 

and other methods ...

 //Closes activity after 10 seconds of inactivity public void onUserInteraction(){ myHandler.removeCallbacks(closeControls); myHandler.postDelayed(closeControls, delayTime); } private Runnable closeControls = new Runnable() { public void run() { finish(); overridePendingTransition(R.anim.fadein, R.anim.fadeout); } }; 
+11
source

To fulfill the answer above, note that Activity.onUserInteraction () is adequate only if you care about clicks.

The documentation at http://developer.android.com/reference/android/app/Activity.html#onUserInteraction%28%29 states: "Note that this callback will be called for a touch action that starts a touch gesture, but not can be called for the touch and touch actions that follow. "

The actual implementation showed that it really ignores all movements on the tablet, which means that the watch will never reset, but, say, a drawing without releasing a finger. On the other hand, this also means that the watch often does not reset, which limits overhead.

0
source

Source: https://habr.com/ru/post/1339595/


All Articles