in one of my Activities I want to keep screen on for 2 minutes (e.g.). I know I can keep screen on with:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
but how to do that for an specific duration ?
You have many ways to clear flags after 2 minutes..like you can use timer or thread or handler
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
getWindow().clearFlags(android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
}, 20000);
in this way you can clear the flags
Take this:
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
// turn screen off function
}
}, 2000);
After 2sec it will turn off the screen. You just have to put the turn off function in it.
Handler handler = new Handler();
// run a thread after 2 seconds to start the home screen
handler.postDelayed(new Runnable() {
#Override
public void run() {
finish();
// start your screen
}
}, 2000); // time in milliseconds (1 second = 1000 milliseconds) until the run() method will be called
Related
I have two functions. The first function takes 8 seconds and the second function takes 5 seconds. I want the second function to run with a delay of 3 seconds and the program not to stop and the first function not to stop.
How can I do this?
Please help me.
Use the Handler method :
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
yourfuction(); //This function will only work after 3 second
}
}, 3000);
Handler handler = new Handler();
Runnable myRunnable = new Runnable() {
public void run() {
// Things to be done
callYourFunction();
handler.postDelayed(this, 3000); //after every 3 sec call your function
}
};
handler.postDelayed(myRunnable, 3000);
I am trying to make feature that will display text on screen and after few sec to disappear. I have managed that with Handler and Timer classes.
The problem is that I need to somehow stop executing these timers if user makes input over keyboard before timer's time pass and rerun timer again to display different data.
I am facing with problem that the view has remaining visible after user input and disappears after 1-2 sec instead after 5 sec.
the codes that I have used:
//do something
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
//do something again
}
}, 5000);
}
AND
//do something
new Timer().schedule(new TimerTask() {
#Override
public void run() {
// this code will be executed after 5 seconds
//do something again
}
}, 5000);
Can you help me to solve this problem?
THanks
For timer you can use timer.cancel(); and for handler you can use handler.removeCallbacks(runnable);
So just declare a Runnable runnable as a global variable, then instantiate it as below
handler.postDelayed(runnable = new Runnable() {
#Override
public void run() {
//do something again
}
}, 5000);
this is the runnable you will pass when you removeCallbacks. Similarly you can use this handler.removeCallbacks(null); this will stop all the handlers that have been declared. I would suggest that you declare both the handler and the timer as global variables and only instatntiate them when you are calling the timer tasks.
I have set a counter and when the counter value is zero the button will be disabled. But I want to re-enable it after 3minutes. so, how am I supposed to set a timer for this?
One option is you could use a handler that will run in 3 minutes
Handler h = new Handler();
h.postDelayed(new Runnable() {
#Override
public void run() {
}
},3_MINUTES_IN_MILIS);
okay. So when you disable the button, you can do this,
button.postDelayed(new Runnable() {
#Override
public void run() {
//enable the button
}
}, 3 * 60 * 1000);
This code should be run from UI/Main thread
When you disable the button, call this code
Handler handler = new Handler();
handler .postDelayed(new Runnable() {
#Override
public void run() {
}
},3 * 60 * 1000);
Let me know if it solved your purpose.
After pushing a button, i want to close an activity. But, I’d like to wait some seconds before closing it, because users have to read a short message displayed on that activity.
I tried using Thread inside the onClick event
try{
Thread.sleep(2000);
finish();
}
catch(Exception e){}
But, when I push the button, the entire objects are freeze (for example, the button stay pushed).
Then I used a simple Timer
timer.schedule(task(), 2000);
And it seems to work well. Is it correct to use a Timer in this situation, or should I use a Thread or something else?
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Activity.this.finish();
}
}, 2000) ;
The easiest way is probably to use a Handler
private Handler h = new Handler();
...
h.postDelayed(new Runnable() {
#Override
public void run() {
finish();
}
}, 2000);
I have created a custom control panel for a video player. Now I want to give a effect like default MediaController where the panel becomes visible when the screen is touched and it becomes invisible again after the last touch time. I can use this type of code for that.
Thread thread = new Thread() {
#Override
public void run() {
try {
Thread.sleep(60000);
} catch (InterruptedException e) {
}
runOnUiThread(new Runnable() {
#Override
public void run() {
// make the panel invisible
}
});
}
};
I can start the thread when the screen is touched and make it invisible after 60 seconds. But in my case, if the user touches the screen again in between this 60 seconds, the panel should vanish after 60 seconds from the last touch. How to consider this case also?
I would recommend using a combination of Runnables and a Handler. You can do Handler calls using postDelayed() to do something after, say, 60 seconds.
Here's an example:
private Handler mHandler = new Handler();
mHandler.post(showControls); // Call this to show the controls
private Runnable showControls = new Runnable() {
public void run() {
// Code to show controls
mHandler.removeCallbacks(showControls);
mHandler.postDelayed(hideControls, 60000);
}
};
private Runnable hideControls = new Runnable() {
public void run() {
// Code to hide the controls
}
};
Simply delete/cancel current timer.
Btw, you should not do it by Thread, but by posting message to a Handler. Such future timer task doesn't need another thread.