Timer switch android TimerTask - android

b= new TimerTask()
{
#Override
public void run()
{
out_string = hi.getWebPage("http://daniandroid.honor.es/getAllCustomers.php");
frum_timer = Integer.parseInt(out_string.replaceAll("[\\D]",""));
};
};
t.scheduleAtFixedRate(b, 500, 1000);
Every 1000 ms, run() will store the value to int frum_timer
I'm using this for an flashlight application.
When I open the app, I want my app to switch on and off flashlights using frum_timer for interval time.
So if
frum_timer = 1000;
my flashlight will turn on and off every 1 sec.
and if I edit to
frum_timer = 300;
it will turn on and off every 0.3 sec.
I tried instantiate a new TimerTask()
c=new TimerTask()
{
#Override
public void run()
{
timetask = new TimerTask()
{
boolean checking = false;
#Override
public void run()
{
runOnUiThread(new Runnable()
{
#Override
public void run()
{
if(checking==true)
{
//camera turning on
checking=false;
}else{
//camera turninf off
checking=true;
}
}
});
}
};
}
};
//some code
t.scheduleAtFixedRate(c, 50, 700);
but it's quite hard, because when I change the value, a new timer will start and the old one will remain active.
BTW: I know that the code for turning on/off will not change at the same interval as I want because of my amateur programmer skills (if checking...etc)

I have found a way.
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();

Related

Can't get coundown timer to work or display in my program

I'm a newer programmer and this is my first project but I'm having a bit of trouble in making a proper loop with three timers that are supposed to run one after the other. I managed to get the objects to hold the values they are supposed to within the loop but for some reason, the timer isn't displaying in the text field like it should.
startBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i("mTimer:", String.valueOf(mTimer));
Log.i("mReps:", String.valueOf(mReps));
Log.i("Flexion:", String.valueOf(flexionTimer));
Log.i("Hold:", String.valueOf(holdTimer));
Log.i("Extension:", String.valueOf(extensionTimer));
for (int iter = 0; iter < mReps; iter++) {
Log.i("Loop:", String.valueOf(iter));
final Timer workingFlexionTimer = new Timer();
workingFlexionTimer.schedule(new TimerTask() {
int counter = ((int) flexionTimer / 1000);
#Override
public void run () {
runOnUiThread(new Runnable() {
#Override
public void run() {
mPhase.setText("Flexion");
mCountDownTimer.setText("" + String.format(String.valueOf(counter + 1)));
}
});
if (counter-- == 0) {
workingFlexionTimer.cancel();
}
}
}, 0, 1000);
final Timer workingHoldTimer = new Timer();
workingHoldTimer.schedule(new TimerTask() {
int counter = ((int) holdTimer / 1000);
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
mPhase.setText("Hold!!!");
mCountDownTimer.setText("" + String.format(String.valueOf(counter + 1)));
}
});
if (counter-- == 0) {
workingHoldTimer.cancel();
}
}
}, flexionTimer, 1000);
final Timer workingExtensionTimer = new Timer();
workingExtensionTimer.schedule(new TimerTask() {
int counter = ((int) extensionTimer / 1000);
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
mPhase.setText("Extension");
mCountDownTimer.setText("" + String.format(String.valueOf(counter + 1)));
}
});
if (counter-- == 0) {
workingExtensionTimer.cancel();
}
}
}, (flexionTimer + holdTimer), 1000);
}
I'm kind of at a loss at this point and any suggestion would be appreciated.
Use handler
private void Timer() {
handler = new Handler();
Run =new Runnable() {
#Override
public void run() { //Do something after 10 sec
Toast.makeText(getActivity(), "Timer called!",Toast.LENGTH_LONG).show();
Timer(); // Do again
}};
handler.postDelayed(Run , 10000); // 10 sec
}
UPDATE
For timers I always do:
final TextView textView = (TextView)findViewById(R.id.textView);
Timer timer = new Timer();
timer.schedule(new TimerTask() {
int counter = 10;
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
textView.setText(""+counter);
}
});
if (counter-- == 0){
timer.cancel();
}
}
}, 0, 1000);
timer.schedule(new TimerTask() {
int counter = 10;
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
textView.setText(""+counter);
}
});
if (counter-- == 0){
timer.cancel();
}
}
}, 10000, 1000);
timer.schedule(new TimerTask() {
int counter = 10;
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
textView.setText(""+counter);
}
});
if (counter-- == 0){
timer.cancel();
}
}
}, 20000, 1000);
For more info check this
In the link...
public void schedule(TimerTask task, long delay, long period)
The above code. Start the run() without delay.
long delay = 0;// in ms
long period = 1000;// in ms
So every 1000ms call the run() and counter--. When counter = 0 cancel.
If you want to run, the one after the other, put delay.
UPDATE
Now the first will run immediately, the second will wait 10000ms (10s) and will run, finaly the third will wait 20000ms (20s) and then run.
The first timer flexionCountDownTimer was started in a loop, which means it will be started more than one time if mReps is greater than 1. This might leads to 2nd run of flexionCountDownTimer before the 1st run finished. Is this your expected behavior?

Android Asynk Task run in thread

i want to run asynk task in every 5 seconds till 5 minuts, how can i do this ?
i am able to run asynk task in every 5 seconds but can no limit 5 minuts
public void callAsynchronousTask() {
final Handler handler = new Handler();
Timer timer = new Timer();
TimerTask doAsynchronousTask = new TimerTask() {
#Override
public void run() {
handler.post(new Runnable() {
public void run() {
try {
new CheckTxnStatusSTResult().execute(final_verification_card);
Log.d("dinesh","execute in every five seconds"+ final_verification_card);
} catch (Exception e) {
}
}
});
}
};
timer.schedule(doAsynchronousTask, 0, 15000);
}
Just you can do like this
public void FunctionName() {
new CountDownTimer(Total_time, 1000) {
#Override
public void onTick(long millisUntilFinished) {
MyUtils.Log("execute in every one seconds");
// Display Data by Every Ten Second
}
#Override
public void onFinish() {
// Call Other function on Function
}
}.start();
}

Disable edit text field in android for 30 seconds

I am making a login screen for an android app. If a user enter wrong password credential 3 times, then the edit text field will be disable for 30 second. After 30 seconds, user can input their password again. How can i achieve it? Thank you
you can try
//Disable your EditText
Handler handlerTimer = new Handler();
handlerTimer.postDelayed(new Runnable(){
public void run() {
// Enable it
}}, 30000);
But I think it's not a good solution to block an edit text 30 seconds, why would you do that?
I hope that help you.
int start1=18000;
final Timer timer = new Timer();
new Thread(new Runnable() {
#Override
public void run() {
timer.schedule(new TimerTask() {
#Override
public void run() {
activity.runOnUiThread(new Runnable() {
#Override
public void run() {
if (start1 <= 0) {
timer.cancel();
timer.purge();
editTextActivation.setEnabled(true);
}
start1 = start1 - 1000;
}
});
}
}, 0, 1000);
}
}).start();
Well first you'll have to have a counter that increments every time a wrong password is submitted. A simple int field would do the trick:
int passwordAttempt = 0;
Next, you'll have to increment this "counter" when a user has entered a wrong password. If the counter reaches 3, disable your EditText (and re-enable it after 30 seconds):
if(passwordAttempt == 3) {
// User has entered wrong password 3 times.
// Disable your EditText.
yourEditText.setEnabled(false);
// Re-enable your EditText after 30000 milliseconds (30 seconds).
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
yourEditText.setEnabled(true);
}
}, 30 * 1000);
// Reset the counter.
passwordAttempt = 0;
}
Try this out...
Thanks to #ridsatrio, I have UPDATED the code.
editText.setEnabled(false);
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setVisibility(View.GONE);
editText.setEnabled(true);
}
}.start();

Timer stop after one minute

I made a Timer and I want to stop it when it reaches to 60 seconds(1 min).
here's the code:
Timer t = new Timer();
t.scheduleAtFixedRate(new TimerTask() {
public void run() {
runOnUiThread(new Runnable() {
public void run() {
myTextView.setText("timer=" + String.valueOf(TimeCounter));
TimeCounter++;
}
});
}
}, 0, 1000);
int I=60;
if (TimeCounter == I) {
-------------- stop the timer here ----------------
}
}
how can I do it?
you will probably need to move the stopping condition inside your task... so it looks more less like this:
final Timer t = new Timer();
t.scheduleAtFixedRate(new TimerTask() {
public void run() {
runOnUiThread(new Runnable() {
public void run() {
if (TimeCounter == I) {
t.cancel();
return;
}
myTextView.setText("timer=" + String.valueOf(TimeCounter));
TimeCounter++;
}
});
}
}, 0, 1000);
Remember to define earlier:
int I=60;
Also, you would probably need to mark Timer as final (just as I did in code).
try below code to stop timer :-
if (TimeCounter == I) {
t.cancel();
}
also see below link:-
How to stop immediately the task scheduled in Java.util.Timer class
You can use CountDownTimer, maybe this is a simpler solution:
int Time = 0;
public class OneMinuteCountDownTimer extends CountDownTimer {
public OneMinuteCountDownTimer (long startTime, long interval) {
super(startTime, interval);
}
#Override
public void onFinish() {
myTextView.setText("timer="+Time+" time finished");
Time=0;
}
#Override
public void onTick(long millisUntilFinished) {
Time++;
myTextView.setText("timer="+Time);
}
}
}
Make the CountDownTimer global and call it when You want to start the CountDownTimer:
private OneMinuteCountDownTimer countDownTimer;
private final long startTime = 60 * 1000;
private final long interval = 1 * 1000;
inside onCreate (EDIT):
countDownTimer = new OneMinuteCountDownTimer(startTime,interval);
countDownTimer.start();
and cancel it, if You want to cancel it before one minute:
countDownTimer.cancel();

Android Timer And UI Thread

I inflate a TableRow like this:
final TableRow tr1 = (TableRow)LayoutInflater.from(this).inflate(R.layout.attrib_row_survey, null);
tr1.setOnTouchListener(this);
tl1.addView(tr1);
This is how I do my counter:
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
((TextView)tr1.findViewById(R.id.textView2)).setText("Seconds Remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
((TextView)tr1.findViewById(R.id.textView2)).setText("DONE");
}
}.start();
The problem is, when I run this timer and show it in the row, the UI is very slow and the timer lags. When I do a view.setBackGroundColor(Color.BLACK) in the onTouchListener() and I click on it, it lags.
protected static void startTimer() {
isTimerRunning = true;
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
elapsedTime += 1; //increase every sec
mHandler.obtainMessage(1).sendToTarget();
}
}, 0, 1000);
}
public Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
StopWatch.time.setText(formatIntoHHMMSS(elapsedTime)); //this is the textview
}
}

Categories

Resources