Interrupting execution of handler/timer if user input occurs Android - android

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.

Related

How to execute a function with a delay of a few seconds in Android programming

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);

Setting Interval For Activiry and Perform Task

Is there any function in Android that can use to make the activity wait for an interval and continue working?
I mean , for example, I use setContentView() to set a layout , and after 3 seconds it will load another layout, and continue to do another job, I don't need to repeat doing same thing after an interval, just continue do another thing.
Thanks in advanced.
You can Use Following Method to Set Interval
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
// This method will be executed once the timer is over
// Do your code here
}
}, 3000);
Here, 1000 = 1 Second
But Before running this code make sure that you are not in BACKGROUND PROCESS THREAD...otherwise this may cause an error..
Do it at Android Style:
Handler mHandler = new Handler();
mHandler.postDelayed(runnableHandler, 3000);
private Runnable runnableHandler = new Runnable() {
#Override
public void run() {
doSomething()
}
};
private void doSomething() {
// Before do something remove all callbacks from Handler
mHandler.removeCallbacks(runnableHandler);
andFinallyDoWhatYouNeed();
}

how to keep screen on for an specific duration?

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

Closing an activity after a number of second

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);

How to Delay Execution of Code For X Amount of Time in Android

On button click I want to begin a timer of 5 minutes and then execute a method that will check for certain conditions and set off alerts if conditions are right. I've seen examples with timers and postDelay, but don't really understand why one would use one vs another. What is the best way to accomplish what I am trying to do? I don't want to lock up the UI during the 5 minutes. The user should be free to use the app as normal during the countdown.
EDIT: I am trying the postDelayed suggestion but visual studio is not liking something about my code. It looks exactly like examples I've found. My be a mono for android thing.
Handler h = new Handler();
Runnable r = new Runnable(){
public void run()
{
Dialog d = inst2.showBuilder(this, "test", "test");
d.Show();
}
};
h.postDelayed(r, 5000);
Specifically the code block inside of run throws all kinds of "} expected" and "a namespace cannot directly contain members such as fields or methods" exceptions.
Try using Timer Object :
Timer mTimer = new Timer();
mTimer.schedule(new TimerTask()
{
#Override
public void run()
{
// Your code goes here
}
}, 1000); // 1sec
final Handler handler = new Handler();
Timer timer = new Timer();
timer.schedule(new TimerTask()
{
#Override
public void run()
{
handler.post(new Runnable()
{
public void run()
{
// YOUR Code
}
});
}
}, 1000); // 1sec
You can start a simple Thread that will sleep in background for 5 minutes and then call a function. While the thread sleeps in background the UI will not freeze. When the thread finish executing what you want you can set off alerts by sending some intents as notifications and receive them in some Broadcast Receivers.
Hope this helps
Use Handler.postDelayed(Runnable block); method to execute delay, as android also not recommend to use timer.
Handler h = new Handler();
Action myAction = () =>
{
// your code that you want to delay here
};
h.PostDelayed(myAction, 1000);

Categories

Resources