i have a while loop that i need to delay but i cant.
i have already tried two method but neither seem to work.
here's what i tried. I need when i click the button mNavigate to start the loop and run it with delay. I declare above that mNavMode is false
mNavigate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG, "onClick: clicked NAV icon");
mNavMode = !mNavMode;
while (mNavMode){
Log.d(TAG, "while: running");
new CountDownTimer(10000, 2500) {
public void onFinish() {
Log.d(TAG, "onFinish: finish");
}
public void onTick(long millisUntilFinished) {
if (mNavMode){
getDevLoc();
}
Log.d(TAG, "onTick: ticked "+millisUntilFinished);
}
}.start();
}
}
});
mNavigate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG, "onClick: clicked NAV icon");
mNavMode = !mNavMode;
Handler handler1 = new Handler();
while (mNavMode){
handler1.postDelayed(new Runnable() {
#Override
public void run() {
getDevLoc();
}
}, 1000 );
Log.d(TAG, "while: running");
}
}
});
I've used the second option using a Handler, it should work. But it appears that your while condition is false, just before starting a loop you set mNavMode = !mNavMode, and if your mNavMode is set to true beforehand than your loop body is never executed.
Related
I am a beginner in Android. I have created a chronometer and it works. I added a new function (every 5 minutes a value 1000 shown in the textview, after 10 minutes it takes 2000). This function works but if I click a button exit and I re-open the chronometer, after 5 minutes the value continues to add to the previous value but I want to reset this value once I click exit. I have used
finish(); handler.removeCallbacks(null); handler.postDelayed(test, 100); but it doesn't seem to work. Thanks.
btnStart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (lastPause != 0) {
chronometer.setBase(chronometer.getBase() + SystemClock.elapsedRealtime() - lastPause);
} else {
chronometer.setBase(SystemClock.elapsedRealtime());
}
chronometer.start();
suppl();
btnStart.setEnabled(false);
btnStop.setEnabled(true);
btnExit.setEnabled(false);
}
});
btnStop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
chronometer.stop();
onPause();
lastPause = SystemClock.elapsedRealtime();
btnStop.setEnabled(false);
btnStart.setEnabled(false);
btnExit.setEnabled(true);
}
});
btnExit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
handler.removeCallbacks(null);
handler.postDelayed(test, 100);
}
});
}
Handler handler = new Handler();
Runnable test;
public void suppl() {
test = new Runnable() {
#Override
public void run() {
count += 1000;
tprice.setText(String.valueOf(count));
handler.postDelayed(test, 5000);
}
};
handler.postDelayed(test, 5000);
}
public void onPause() {
super.onPause();
if (handler != null)
handler.removeCallbacks(test);
}
I would suggest using a TimerTask for this purpose.
While using time task you can cancel all the pending tasks by using cancel() on the timer.
if you have any doubts in this solution, let me know.
Instead use CountDownTimer
CountDownTimer CD = new CountDownTimer(11000, 1000) {
public void onTick(long millisUntilFinished) {
xo = (int) (millisUntilFinished / 1000);
secondsleftimg.setText("seconds remaining: " + millisUntilFinished / 1000);
Try this out :
Replace this line of code :
handler.removeCallbacks(null);
with this :
handler.removeCallbacksAndMessages(null); // it will remove all hadler
btnExit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
focus.setBase(SystemClock.elapsedRealtime());
handler.removeCallbacksAndMessages(null);
lastPause= 0;
}
});
Hi i have a button from which i call the given methods the problem is when i click button for the first time TextToSpeach Player doesn't play the sound but handler code execute perfectly.If i click on button again everything work perfectly means TextToSpeach play the sound.Thank you..
public void selectDest() {
TextToSpeechPlayer.playSound("hello");
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Log.d(TAG, "After 1 sec ");
}
},1000);
}
Try this
TextToSpeechPlayer.playSound("hello");
handler = new Handler();
runnable = new Runnable() {
public void run() {
//do your other task here
handler.postDelayed(this, 60 * 1000);
}
};
handler.postDelayed(runnable, 0);
public void selectDest() {
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
TextToSpeechPlayer.playSound("hello");
Log.d(TAG, "After 1 sec ");
}
},1000);
}
I have implemented one counter and onFinish() of first counter,I started second counter but the first counter not able to finish.Text "Bye Guyz" remain for some time so how to finish the text.
Please help me.
Thanks in advance.!!!
Code :-
counter= new CountDownTimer(10000, 1000) {
public void onTick(long millisUntilFinished) {
if (count == 0) {
tv.setText("First counter");
tv2.setVisibility(View.VISIBLE);
tv2.setText("Hello Guyz");
}
}
public void onFinish() {
if(!flag) {
tv2.setText("Bye Guyz");
count = 0;
try {
counter.cancel();
}catch (Exception e){}
}
else if(flag) {
counter1 = new CountDownTimer(9000, 1000) {
public void onTick(long millisUntilFinished) {
flag = false;
tv.setText("Second counter");
tv2.setVisibility(View.VISIBLE);
tv2.setText("Hello Girls");
count = 0;
}
public void onFinish() {
tv2.setVisibility(View.VISIBLE);
tv2.setText("Bye Girls");
count = 0;
}
}.start();
Did you "debug" the code to be sure the code is arriving to counter1 = new CountDownTimer(9000, 1000)?
Are you sure when the first counter arrives to onFinish() the flag variable is true?
Why do you call counter.cancel() in onFinish() when obviously the counter is already over?
public void onFinish() {
if(!flag) {
tv2.setText("Bye Guyz");
count = 0;
try {
counter.cancel();
}catch (Exception e){}
}
If you say your tv2 displays "Bye Guyz" it means that your flag is set to false, so the "else if" part is not being executed. onFinish() is only executed once, so you need to make sure the flag is set for true to start the second counter.
Also you shouldn't cancel your counter in onFinish() because it's already finished.
Here is my alternative is as follows
Create the custom Counterextending Thread
class Counter extends Thread {
private long timeOne, timeTwo;
private OnCounterFinishedListener mCounterFinishedListener;
private Thread t;
Activity activity = null;
Counter(Context context){
t = new Thread(this);
activity = (Activity)context;
}
#Override
public void run() {
try {
sleep(timeOne);
activity.runOnUiThread(new Runnable() {
#Override
public void run() {
mCounterFinishedListener.firstCounterFinished();
}
});
sleep(timeTwo);
activity.runOnUiThread(new Runnable() {
#Override
public void run() {
mCounterFinishedListener.secondCounterFinished();
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
}
void setTimes(long timeOne, long timeTwo){
this.timeOne = timeOne;
this.timeTwo = timeTwo;
}
public void start(OnCounterFinishedListener listener){
mCounterFinishedListener = listener;
t.start();
}
interface OnCounterFinishedListener{
void firstCounterFinished();
void secondCounterFinished();
}
}
Then inside your main thread you can start this counter as
final Counter counter = new Counter(this);
counter.setTimes(5000, 5000);
counter.start(new Counter.OnCounterFinishedListener() {
#Override
public void firstCounterFinished() {
// Update your first TextView
}
#Override
public void secondCounterFinished() {
// Update your second TextView
}
});
can any 1 give me example to perform onClick on button for multiple times when user clicked for 1 time. when i click on button 1 time it should automatically click after delay of 5 seconds for 100 times. how to perform that.
This is my sample code
mUnlock.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//It should be already ensured that this mSelectedLock is something user is authorized to access
if (mSelectedLock.unlock("RANDOM")) {
mUnlock.setVisibility(View.INVISIBLE);
mUnlock.postDelayed(new Runnable() {
public void run() {
mUnlock.setVisibility(View.VISIBLE);
}
}, 5000);
} else {
Toast.makeText(MainActivity.this, "Unable to unlock.", Toast.LENGTH_LONG).show();
}
}
});
#Override
public void onClick(View v) {actionToBeDone();startLoop(0);}
private void startLoop(final int i) {
if(i!=100) {
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Log.e("i",""+i);
actionToBeDone();
startLoop(i+1);
}
}, 2000);
}
}
private void actionToBeDone() {
//enter actions you want to be done
Log.e("actionToBeDone","Button Action");
}
int count = 0;
Timer timer = new Timer();
timer.schedule(new TimerTask(){
#Override
public void run(){
if(count < 100){
mUnlock.performClick();
}
}
}, 0, 5000);
5000 is the time in miliseconds you can +/- from here.
I want to print sequential numbers every 2 seconds when I press button. I used following code:
int j=0;
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
c=Calendar.getInstance();
Delay(2 ,c.get(Calendar.SECOND));
if(j++<5)
t.setText("number "+j);
}
});
public void Delay(int p,int q){
int z=0;
while(z<p){
c=Calendar.getInstance();
i= c.get(Calendar.SECOND);
z=i-q;
}
return ;
}
but this code prints "Number 5" at the end of 10 seconds directly.
How can I print "Number 1", "Number 2", "Number 3"....sequentially every 2 sec.
Note that if you're doing this on UI thread, you'll be blocking the UI thread for 10 seconds. It's much better to have a separate thread to do this:
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
new Thread() {
public void run() {
for(int j=1; j<=5; i++) {
runOnUiThread(new Runnable() {
#Override
public void run() { t.setText("number " + j); }
});
SystemClock.sleep(2000);
}
}
}.start();
}
});
This code starts a new thread (thus not blocking the UI), which iterates from 1 to 5 outputting the number (on UI thread, as it's changing the UI) and then sleeps for 2 seconds.
Use a Runnable to post to a handler bound to the UI thread for your app rather than sleep or delay. If you sleep or delay in your onClick() method, you are blocking the UI thread and it will make your UI unresponsive.
public class MyActivity extends Activity implements Handler.Callback {
...
private Handler mHandler = new Handler(this);
private int mNumber = 0;
...
#Override
public void onClick(View v) {
mNumber++;
mHandler.postDelayed(new Runnable() {
public void run() {
t.setText("number: " + mNumber);
}, 2000);
}
}
You can use the CountDownTimer for this..you just have to define the amount of time and how often you want to update. You just have to adjust the logic a bit to do print the numbers upward.
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
new CountDownTimer(60000, 2000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();
}
});