I have a problem that i can't work out. I have a button that when clicked changes the text view. It then activates a postdelayed process that returns the textview to its original text after 2 seconds.
If i press the button once, and then again within this 2 second interval the postdelay will continue to count down from the first press and not restart itself from the second press. This results in the original text being shown when i want the changed text to be.
Each time the button is pressed it creates a delay from that instance. I want it to cancel the previous postdelay and start a new one. This is my code so far but its not finished because i can't work out how to finish it (so it does not work).
p1AddL.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
counter1 ++;
count1 ++;
Handler h = new Handler();
if ('PREVIOUS_DELAY_HAS_STARTED') {
h.removeCallbacks(clickButton);
h.postDelayed(clickButton, 2000);
} else {
h.postDelayed(clickButton, 2000);
}
if (count1 > 0) {
lifepointsP1.setText("+" + count1);
lifepointsP1.setTextColor(Color.argb(220, 0, 188, 0));
}
}
});
Runnable clickButton = new Runnable() {
#Override
public void run() {
count1 = 0;
lifepointsP1.setTextColor(Color.WHITE);
lifepointsP1.setText(counter1);
}
};
the PREVIOUS_DELAY_HAS_STARTED text needs to be some sort of checking method and i'm pretty sure i need something between the h.removeCallbacks and h.postDelayed commands under that text.
If their is a simpler way/better way to write this method to make it work please let me know. I have tried so many ways and i feel i am very close here.
removeCallbacks won't do anything if clickButton isn't registered on h. So you can simply replace
if ('PREVIOUS_DELAY_HAS_STARTED') {
h.removeCallbacks(clickButton);
h.postDelayed(clickButton, 2000);
} else {
h.postDelayed(clickButton, 2000);
}
with
h.removeCallbacks(clickButton);
h.postDelayed(clickButton, 2000);
Related
I defined a Runnable which starts after a button clicked,
it changes image of a ImageView every 1 second.
There is an other button in my view that when user click on that, again I start that Runnable but this time it changes image of ImageView every 2 seconds and so on.
My question is: when I click on second button, now do I have to individual thread running or it just stop first instance of Runnable and start the other instance?
Note: I read some topics about threads and Runnable in android but still the different is not clear for me.
Also reading this question and it's answers head me hear to ask my own question.
Java - Running a thread twice
Sorry for bad English.
Runnable part of my code:
private final Runnable mRunnable = new Runnable() {
public void run() {
if (mIsFlashOn) {
if (mSwap) {
mImageViewBeam.setVisibility(View.VISIBLE);
mSwap = false;
mHander.postDelayed(mRunnable, 10000 / ((mStrobeCounter * 5) + 10));
} else {
mImageViewBeam.setVisibility(View.GONE);
mSwap = true;
mHander.postDelayed(mRunnable, 10000 / ((mStrobeCounter * 5) + 10));
}
}
}
};
I think you need to use mHandler.removeCallbacks(mRunnable); this line to remove first runnable , and than start another
So from your code what have I understood is, you're creating a thread inside a while loop which is updating the ImageView over the time. If I've understood correctly then its really a bad practice to do so. I might be wrong. Please post your updating code in that case. How you're updating the ImageView?
If I'm right, try considering CountDownTimer which will serve your purpose I hope.
CountDownTimer mCountDownTimer = new CountDownTimer(howLongYouWantItToRunInMilis, intervalInMilis) {
public void onTick(long remainingMilis) {
// Update your ImageView here
if (mSwap) {
mImageViewBeam.setVisibility(View.VISIBLE);
mSwap = false;
} else {
mImageViewBeam.setVisibility(View.GONE);
mSwap = true;
}
}
public void onFinish() {
// Start the timer again from the code.
}
}.start();
Set the intervalInMilis when the button is clicked. Then mCountDownTimer.cancel() to cancel the timer and mCountDownTimer.start() to start it again immediately.
I have the Problem that my Android app does not delay a second (or 10 seconds), if I use the postDelayed method..
Basically I would like my program to wait one second after I clicked the button, then update the text on my textview ("READY"), wait another 2 seconds, then update the textview again ("SET") and then it should start another activity (not yet implemented :-) ).
With my code, the programm starts and after I click the button the textview shows the last text ("SET") immediately.. It just does not wait.
What am i doing wrong?
Here is my code:
public class MyCounterActivity extends Activity {
private long mInternval = 100000;
private Handler mHandler;
private Runnable mStatusChecker = new Runnable() {
#Override
public void run() {
//updateInterval(); //change interval
startRepeatingTask();
}
};
void startRepeatingTask(){
mHandler.postDelayed(mStatusChecker, mInternval);
//mStatusChecker.run();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gym_counter);
final TextView tv1 = (TextView) findViewById(R.id.fullscreen_content);
final Button startButton = (Button) findViewById(R.id.startbutton);
startButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final long up;
EditText textUp = (EditText) findViewById(R.id.editTextUp);
up = Integer.parseInt(textUp.getText().toString());
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
//
}
},1000);
Log.d("after 1 runnable", "whaaat");
tv1.setText("Ready");
handler.postDelayed(new Runnable() {
#Override
public void run() {
//
}
}, 2000);
Log.d("after 2nd runnable", "whaaat 2");
//startRepeatingTask();
tv1.setText("SET");
}
});
}
I also tried to run it with the runOnUiThread() (within the onClick(View v) but with with the same result). I expected it to wait 1 second (startRepeatingTask()) and then runs the loop and waits several seconds...
runOnUiThread(new Runnable() {
#Override
public void run() {
startRepeatingTask();
for (int u = 0; u < up; u++){
startRepeatingTask();
}
}
}
});
Hope my description makes sense :-).
Thank you for your help!
EDIT:
I was now able to find a solution for my first problem. The answer from #mad in this post helpded me: How to start a different activity with some delay after pressing a button in android?
(Thats probably the same thing that #laalto tried to tell me. Thanks for the hint!)
In the onClick()
tv1.setText("READY");
mHandler.postDelayed(mDelay1, 2000);
And then the Runnable
private Runnable mDelay1 = new Runnable() {
#Override
public void run() {
if (tv1.getText()=="READY")
tv1.setText("SET");
}
};
BUT:
If i want to refresh the text on my Textview after every second, how do i do that? I cant just call mHandler.postDelayed() several times.. Any help is appreciated.
When you call postDelayed(), it just places the Runnable in a queue and returns immediately. It does not wait for the runnable to be executed.
If you need something to happen after a delay, put the code in the run() method of the runnable.
Whenever you call something like Thread.start(), handler.postDelayed, view.postDelayed, AsynchTask, TimerTask .. you enter the world of threading or you might call it parallel computing.
So there can be multiple threads ("codes") running at the same time.
When you are inside your Activity it is running in a Thread that is calld UI-thread or main thread. All graphics is handled in that thread and that thread alone.
Do NEVER wait in the UI-thread!
Example: you have a button that switches color from say gray to yellow on pressing it. Now you enter a Thread.sleep(10000); - waiting 10 seconds at the start of your onClick.
You will then see that the button stays yellow (=pressed) for 10 seconds even if you only pressed very shortly. Also: if you overdo it android os will become angry and post the user if he wants to force-close your app.
So what happens on handler.postDelayed?
Android will very quickly open a thread that runs in the background parallel to your UI thread. So in some nanoseconds it has done that and will execute the next command in UI thread (in the example above it is Log.d). In the background it will wait and count the millis until time is up. Then any code that is inside the runnable.run method will again be executed in the ui-thread after the wait.
Note also: postDelayed will not be super precise with the wait time as usually the ui-thread is quite buisy and when the wait time is up it may have something else to do. Your runnable code will be added to a queue and executed when ui-thread is ready again. All this happens without you having anything to do about it.
Also:
Remember to work with try/catch inside the runnable.run as many things can happen while waiting - for example user could press Home button closing your app - so the ui-element you wanted to change after the wait could already been destroyed.
I'm working on a math game which presents the player with a simple math problem of the sort 4 + 3 = ? or 6 / 2 = ?. The player is offered 4 possible answers and clicks one. The screen has two buttons, one with an arrow, and one with a square.
The player selects the answer they think is correct. If the answer is correct they get a message. If the answer is incorrect, that choice is grayed out, and they can keep choosing until they get it right.
I want two modes of play: practice and timed.
In practice mode, the player clicks the arrow button and gets a new question. This works fine.
I also want a timed play mode. When the player presses the arrow button, a timer starts and the first problem comes up. If they get it right, they are immediately asked a new question. Again, if they are incorrect, they keep asking until they get it right. Meanwhile, there should be a timer counting down. Play stops when the timer ends or the player presses the square button.
I don't know how to do the timed play where there are two simultaneous activities: playing the game, and the timer.
I'm including pseudo-code instead of code -- the code is long. But if necessary, I can post the entire code.
Update:
This was my naive attempt. The problem is that I can't figure out how to wait in the while loop for until the player to ask the question before askQuestion() is called again. Perhaps it needs to be in a separate thread and have the while loop wait for that thread to return. Would the timer keep going? I want the timer to run simultaneously to a separate task in which a question is posed --> Player answers the question --> a new question is posed .....
public void playTimed()
{
// Toast.makeText(getApplicationContext(), "in playPractice", Toast.LENGTH_SHORT).show();
go_button.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
go_button.setEnabled(false);
new CountDownTimer(10000, 1000) {
public void onTick(long millisUntilFinished)
{
timer.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish()
{
timer.setText("done!");
go_button.setEnabled(true);
timer_done = true;
}
}.start();
while(!timer_done)
{
askQuestion();
}
}
});
}
onCreate()
{
-For all four answer buttons:
answer_button.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
correct = checkAnswer(answer_button_id);
}
});
-get intent extras to know which game
-swith-case to play selected game
}
checkAnswer()
{
if (correct)
{
// feedback
handleRightAnswer();
}
else
{
// feedback
handleWrongAnswer();
}
}
askQuestion()
{
-choose type of problem (+,-,*,/)
-generate arguments and answer
-create random answer list
where one choice is correct
-display problem
-display answer choices
}
playTimed()
{
-When player presses go_button,
timer starts and first question
appears. When correct answer is
selected a new question immediately
appears. Continues until timer runs
out or player presses stop_button.
Seconds left is displayed during play.
}
playPractice()
{
//When player presses go_button
// a new question appears.
go_button.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
askQuestion();
}
});
}
As Gabe Sechan stated, you can do this using a timer to update a counter, then when the counter hits 0, do something. You can also use the timer to update a TextView (to display the time remaining).
Here is a class that I have used in the past, originally found here:
public class UIUpdater{
private Handler mHandler = new Handler(Looper.getMainLooper());
private Runnable mStatusChecker;
private int UPDATE_INTERVAL = 1000; //updates every second
public UIUpdater(final Runnable uiUpdater) {
mStatusChecker = new Runnable() {
#Override
public void run() {
// Run the passed runnable
uiUpdater.run();
// Re-run it after the update interval
mHandler.postDelayed(this, UPDATE_INTERVAL);
}
};
}
public UIUpdater(Runnable uiUpdater, int interval){
this(uiUpdater);
UPDATE_INTERVAL = interval;
}
public synchronized void startUpdates(){
mStatusChecker.run();
}
public synchronized void stopUpdates(){
mHandler.removeCallbacks(mStatusChecker);
}
}
Then, in your method where you want to update your counter, you would put:
UIUpdater mUIUpdater = new UIUpdater(new Runnable() {
#Override
public void run() {
//method to update counter
}
});
//to start the method:
mUIUpdater.startUpdates();
//to stop the method, ie, when counter == 0
mUIUpdater.stopUpdates();
I just want to call a function after every 3secs on click of a button
What is going wrong here-
galleryBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
final Handler handler = new Handler();
for(int i = 0;i<3;i++){
handler.postDelayed(new Runnable() {
#Override
public void run() {
// Do something after 5s = 5000ms
viewAnimator.showNext();
}
}, 3000);
}
}
});
You don't actually say what goes wrong but I'll take a wild guess that nothing happens (i.e. no animations) and the reason for that is probably that your Handler is being GC'd long before it gets to handle anything. Try keeping moving 'handlers' scope from local variable to class member.
(Also note that, even when it works, all 3 of your functions will run at more or less the same time. If you want them to run 3 seconds apart you should change the '3000' to 'i*3000'.)
I have an activity that runs some ASCII control over a network port to a remote device.
Every single button push on the interface will trigger an AsyncTask to handle the communication, and (finally) works great.
However, if a user starts button mashing like a chimp on crack, the system will crash with way too many calls on the same socket, so I've come up with a little timer function to slow down the reaction to their excitement.
I'm wondering if somebody has come up with a better way to do this?
First off, inside the onCreate:
btn_pwrtoggle = (Button)findViewById(R.id.pwr_btn);
btn_pwrtoggle.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(!buttonMasher){
if(powerstat.equals("OFF")){
String[] commandToSend = {"POWER","ON"}
}else{
String[] commandToSend = {"POWER","OFF"};
}
deviceControl(commandToSend);
}
startButtonMashTimer();
}else{
Log.w("button masher","slow down there, monkey.");
}
}
});
Then, in the actual Activity:
Timer buttonTimer;
TimerTask buttonMonitorThread;
int chimpCrackCounter;
protected void startButtonMashTimer() {
chimpCrackCounter = 0;
buttonTimer = new Timer();
buttonMonitorThread = new TimerTask(){
#Override
public void run(){
buttonMasher = true;
if(chimpCrackCounter == 1){
buttonMasher = false;
buttonTimer.cancel();
}
chimpCrackCounter++;
}
};
buttonTimer.schedule(buttonMonitorThread, 0, 500);
}
It seems to be working just fine, (and may help somebody having the same difficulty) but I'm open to suggestions.
An easy way to prevent a user from pushing a button too often is to save the time when a button was pushed, and then next time compare the last time with the current time and if the difference is too small, ignore the action.
final static long minTimeBetweenClicks = 1000;
long lastTime;
onClick(View v){
if( System.currentTimeMillis() < lastTime + minTimeBetweenClicks ) return;
//Handle the click
lastTime = System.currentTimeMillis();
}
The beauty of this is that it doesn't require any new threads or timers, and your AsyncTasks won't have to know about the buttons.
Disable the Button after a click (setEnabled(false), perhaps in onPreExecute, and enable after the task is done, in onPostExecute.
Also, be sure to pay attention to lifecycle changes. Your AsyncTask may be killed if the Activity is paused, so be sure to check the state in onResume.