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);
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!
source share