How to detect no touch on a view after some seconds? - android

I have a small view (call it "user-object") which overlap the background view (call it map). I can drag the "user-object".
I want the "user-object" automatically move to "mid-point" (screen_width/2, screen_height/2) after some seconds (for instance 4s) if three is no touch/drag.

You can send a delay(3000ms) message at the beging and move "user-object" in the Handler's callback,If "user-object" was touched,remove the message

An example using Handler
Handler mHandler;
public void useHandler() {
mHandler = new Handler();
mHandler.postDelayed(mRunnable, 1000);
}
private Runnable mRunnable = new Runnable() {
#Override
public void run() {
// Perform this here
"user-object" automatically move to "mid-point" (screen_width/2, screen_height/2)
mHandler.postDelayed(mRunnable, 1000);
}
After a drag/touch event is triggered, again apply handler.postDelayed().

Related

To set a Time period for enabling and disabling the button

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.

How to use Handler / runnable?

I would like my code runs all the one minut..
Can anyone explain me how to do it?
private Handler myHandler;
private Runnable myRunnable = new Runnable() {
#Override
public void run() {
//DO WORK
Toast.makeText(getApplicationContext(), " Hello",Toast.LENGTH_SHORT).show();
myHandler.postDelayed(this,60000);
}
};
#Override
public void onCreate() {
super.onCreate();
//call function
myHandler = new Handler();
//
myHandler.postDelayed(myRunnable,60000);
Log.d(this.getClass().getName(), "onCreate");
}
watch this http://goo.gl/DRdaUi
BTW 60 second is a very long time for thread to run! If you have something that runs this long consider using android service instead of thread.
Also do you need to use handler? think again, most of the time there is no need for that. AsyncTask is android first option for multithreading and its a lot simpler. unless AsyncTask can not handle what you have in your mind don't use any other method.
A Timer is a valid solution but it will not execute on the UI thread. And your code tells me this is what you want.
The simplest way to do that would be via a handler and a repeating task:
final Handler handler = new Handler(); // ui thread handler
handler.postDelayed(new MyRunnable(handler), INETRVAL);
Where:
class MyRunnable implements Runnable {
#Override
public void run() {
// do periodical action here
// and.. repeat your task <------------
handler.postDelayed(new MyRunnable(handler), INETRVAL);
}
}

What code do i implement to have a time limit in my game

I'm createing a quiz that has a time limit and i dont know what to implement to have a timelimit in my level 1 class. what should i implement? can you show me a complete code?
am i correct?
private Runnable task = new Runnable() {
public void run() {
Intent intent = new Intent(getApplicationContext(),MainMenu.class);
startActivity(intent);
}
};
private void onCreate() {
Handler handler = new Handler();
handler.postDelayed(task, 60000);
There are different ways to do it. One way is to use a Runnable and a Handler.
First, define the Runnable:
private Runnable task = new Runnable() {
public void run() {
Log.i(TAG, "Time limit reached!");
// Execute code here
}
};
Then you call it (say at the start of the level, onCreate) with this Handler and postDelayed
Handler handler = new Handler();
handler.postDelayed(task, 60000);
The code within the run() method of the Runnable will execute 60 seconds after you call postDelayed
If you need regular notifications you can also use a CountDownTimer

Android MediaPlayer custom control panel hide

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.

cancelling a handler.postdelayed process

I am using handler.postDelayed() to create a waiting period before the next stage of my app takes place. During the wait period I am displaying a dialog with progress bar and cancel button.
My problem is I can't find a way to cancel the postDelayed task before the time elapses.
I do this to post a delayed runnable:
myHandler.postDelayed(myRunnable, SPLASH_DISPLAY_LENGTH);
And this to remove it: myHandler.removeCallbacks(myRunnable);
In case you do have multiple inner/anonymous runnables passed to same handler, and you want to cancel all at same event use
handler.removeCallbacksAndMessages(null);
As per documentation,
Remove any pending posts of callbacks and sent messages whose obj is
token. If token is null, all callbacks and messages will be removed.
Another way is to handle the Runnable itself:
Runnable r = new Runnable {
public void run() {
if (booleanCancelMember != false) {
//do what you need
}
}
}
Here is a class providing a cancel method for a delayed action
public class DelayedAction {
private Handler _handler;
private Runnable _runnable;
/**
* Constructor
* #param runnable The runnable
* #param delay The delay (in milli sec) to wait before running the runnable
*/
public DelayedAction(Runnable runnable, long delay) {
_handler = new Handler(Looper.getMainLooper());
_runnable = runnable;
_handler.postDelayed(_runnable, delay);
}
/**
* Cancel a runnable
*/
public void cancel() {
if ( _handler == null || _runnable == null ) {
return;
}
_handler.removeCallbacks(_runnable);
}}
It worked for me when I called CancelCallBacks(this) inside the post delayed runnable by handing it via a boolean
Runnable runnable = new Runnable(){
#Override
public void run() {
Log.e("HANDLER", "run: Outside Runnable");
if (IsRecording) {
Log.e("HANDLER", "run: Runnable");
handler.postDelayed(this, 2000);
}else{
handler.removeCallbacks(this);
}
}
};

Categories

Resources