how can to stop timer task - android

I have a timer and timer task and handler and runnable
my code be execute every 10 seconds until ServerResponse variable in not empty and then redirect to another activity.
but when my code redirect to another activity timer task is working !!!!!
how can to stop timer task when we are in another activity??
Timer timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
new sendDataToServer().execute();
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
if(serverResponse.length() > 0)
{
Intent intent = new Intent(PayementActivity.this,UserFormActivity.class);
startActivity(intent);
finish();
}
}
}, 10000);
}
});
}
}, 0, 10000);

Timer timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
new sendDataToServer().execute();
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
if(serverResponse.length() > 0)
{
Intent intent = new Intent(PayementActivity.this,UserFormActivity.class);
if(timer!= null) {
timer.cancel();
timer.purge();
timer= null;
}
startActivity(intent);
finish();
}
}
}, 10000);
}
});
}
}, 0, 10000);

This might not answer your question directly, but just reactive extensions are a nice alternative to using TimerTasks. Check this and this out.
subscription = Observable.interval(10, TimeUnit.SECONDS)
.subscribeOn(Schedulers.io())// Runs on a io thread pool
.observeOn(AndroidSchedulers.mainThread()) // Observes on UI thread
.subscribe(timeCount ->{
// Your code here
});
subscription.unsubscribe();// Stops the stream.

Related

Button to trigger every 5 seconds

My app needs tracking of real time so I need a button that needs to trigger every 5 seconds but I have no idea how to do it. Can you teach me how?
I want that in every 5 seconds that AsyncTask will be triggered.
btnStart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
HashMap postLoc = new HashMap();
postLoc.put("txtLat", tvLat.getText().toString());
postLoc.put("txtLng", tvLong.getText().toString());
postLoc.put("txtOwner", pref.getString("username","").toString());
PostResponseAsyncTask taskLoc = new PostResponseAsyncTask(getActivity(), postLoc,false, new AsyncResponse() {
#Override
public void processFinish(String s) {
Log.d(TAG, tvLat.getText().toString());
Log.d(TAG, tvLong.getText().toString());
Intent i = new Intent(getActivity(),GPS_Service.class);
getActivity().startService(i);
}
});
taskLoc.execute("http://carkila.esy.es/carkila/locationUpdate.php");
}
});
I think this code might be useful to trigger the code every 5 second
Timer timer;
TimerTask timerTask;
final Handler handler = new Handler();
#Override
public void onCreate() {
super.onCreate();
startTimer();
}
public void startTimer() {
//set a new Timer
timer = new Timer();
//initialize the TimerTask's job
initializeTimerTask();
timer.schedule(timerTask, 0, 5000);
}
public void initializeTimerTask() {
timerTask = new TimerTask() {
public void run() {
handler.post(new Runnable() {
public void run() {
//code to run after every 5 seconds
}
});
}
};
}
Create a method like this and call the method on button click and also call the method by using a handler like this:
mRunnable = new Runnable() {
public void run() {
public void toBecalled_Every_5_Second();
mHandler.postDelayed(mRunnable, 5000);
}
};
mHandler.postDelayed(mRunnable, 5000);
public void toBecalled_Every_5_Second(){
PostResponseAsyncTask taskLoc = new PostResponseAsyncTask(getActivity(), postLoc,false, new AsyncResponse() {
#Override
public void processFinish(String s) {
Log.d(TAG, tvLat.getText().toString());
Log.d(TAG, tvLong.getText().toString());
Intent i = new Intent(getActivity(),GPS_Service.class);
getActivity().startService(i);
}
});
taskLoc.execute("http://carkila.esy.es/carkila/locationUpdate.php");
}
so it will call the method every 5 second and the a sync task will execute....
I would like to have a CountDownTimer which will trigger the button click function after every 5 seconds.
CountDownTimer mTimer = new CountDownTimer(50000, 1000) {
public void onTick(long millisUntilFinished) {
// Do nothing
}
public void onFinish() {
btnStart.performClick();
this.start(); // Restart
}
}.start();
You can use Timer with TimerTask and Handler to update the result to main thread i.e your UI.
Something like this:
Timer timer;
TimerTask timerTask;
//we are going to use a handler to be able to run in our TimerTask
final Handler handler = new Handler();
private void initializeTimerTask() {
timerTask = new TimerTask() {
public void run() {
//use a handler to run process
handler.post(new Runnable() {
public void run() {
/**************************/
/** Do your process here **/
/**************************/
}
});
}
};
}
private void startTimer() {
//set a new Timer
timer = new Timer();
//initialize the TimerTask's job
initializeTimerTask();
//schedule the timer, start run TimerTask then run every 5000ms i.e 5 seconds.
timer.schedule(timerTask, 0, 5000); //
}
private void stopTimerTask() {
//stop the timer, if it's not already null
if (timer != null) {
timer.cancel();
timer = null;
}
}
Insert your processing code in Handler.post(). Then start the trigger by calling startTimer(). To stop the trigger, just call stopTimerTask().

MultiThread in Android for Loop and Delay

As you can see, I have to do TASK1 and continually TASK2. Between 1 and 2, they have delay. And then I want to repeat it infinitely till activity will be finished.
I have no experience for thread.. I can't fix this error.
Help ME!
Timer timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
//TASK 1
main_1.setVisibility(View.VISIBLE);
main_2.setVisibility(View.INVISIBLE);
main_1.startAnimation(AnimationUtils.loadAnimation(getActivity(), android.R.anim.slide_in_left));
Handler mHandler = new Handler(Looper.getMainLooper());
mHandler.postDelayed(new Runnable() {
#Override
public void run() {
//TASK 2
main_1.setVisibility(View.INVISIBLE);
main_2.setVisibility(View.VISIBLE);
main_2.startAnimation(AnimationUtils.loadAnimation(getActivity(), android.R.anim.slide_in_left));
}
}, 1000);
}
}, 1000, 1000);
And it's my error
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
Timer timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
//TASK 1
YourActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
main_1.setVisibility(View.VISIBLE);
main_2.setVisibility(View.INVISIBLE);
main_1.startAnimation(AnimationUtils.loadAnimation(getActivity(), android.R.anim.slide_in_left));
}
});
Handler mHandler = new Handler(Looper.getMainLooper());
mHandler.postDelayed(new Runnable() {
#Override
public void run() {
//TASK 2
YourActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
main_1.setVisibility(View.INVISIBLE);
main_2.setVisibility(View.VISIBLE);
main_2.startAnimation(AnimationUtils.loadAnimation(getActivity(), android.R.anim.slide_in_left));
}
});
}
}, 1000);
}
}, 1000, 1000);
Timer runs on background thread. You can not update ui from background thread, so always use runOnUiThread for that purpose.
Timer timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
YourActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
main_1.setVisibility(View.VISIBLE);
main_2.setVisibility(View.INVISIBLE);
main_1.startAnimation(AnimationUtils.loadAnimation(getActivity(), android.R.anim.slide_in_left));
}
});
Handler mHandler = new Handler(Looper.getMainLooper());
mHandler.postDelayed(new Runnable() {
#Override
public void run() {
//TASK 2
main_1.setVisibility(View.INVISIBLE);
main_2.setVisibility(View.VISIBLE);
main_2.startAnimation(AnimationUtils.loadAnimation(getActivity(), android.R.anim.slide_in_left));
}
}, 1000);
}
}, 1000, 1000);

Repeat "beep" sound each 10 seconds as a service Android

Rx with timer looks like the way to go. If you are not up for it Handler could work as well.
http://reactivex.io/documentation/operators/timer.html
You can try using :
TimerTask scanTask;
final Handler handler = new Handler();
Timer t = new Timer();
public void playBeep(){
scanTask = new TimerTask() {
public void run() {
handler.post(new Runnable() {
public void run() {
repeatBeep();
}
});
}};
t.schedule(scanTask, 10000, 10000);
}
public void repeatBeep(){
mp.start();
}
and call t.cancel() when you want to stop the beep

restarting timer

I would like the Timer to start when I turn ToggleButton ON and to cancel when I turn it OFF. It works, but when I try to start it over again after being cancelled I get an error. Where is the problem? Here is the code:
final ToggleButton btnLive = (ToggleButton)findViewById(R.id.live);
btnLive.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (btnLive.isChecked()) {
timer = new Timer();
timer.schedule(timerTask, 5*1000, 5*1000);
} else {
timer.cancel();
timer.purge();
timer = null;
}
}
});
EDIT: It works with calling timer that way:
timer.schedule(new TimerTask(){
private Runnable runnable = new Runnable() {
public void run() {
new Something().execute();
}
};
public void run() {
handler.post(runnable);
}
}, 5*1000, 5*1000);
Can you explain me why first method is not working? Is it range problem?
Use this type of code and check.
Timer mTimer = new Timer();
mTimer.schedule(new TimerTask()
{
#Override
public void run() {
runOnUiThread(new Runnable() {
public void run() {
}
});
}
}, 5000, 500);

how to change something in a new TimerTask action in android?

i have a problem with the timerTask in android i have a code like this:
timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
countInt = countInt + 1;
textview1.setText(countInt);
}
}, 1000);
every time the timer task get startet my app crashed, i thing because i'm accessing the textview and it is in a other thread right?
how to solve this?
Yes, you are right, it crashes cause' you are accessing views from not an UI thread. To solve this, you can post a Runnable to UI thread using your activity
timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
countInt = countInt + 1;
YourActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
textview1.setText(countInt);
}
});
}
}, 1000);
try this..
timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
countInt = countInt + 1;
yourActivity.this.runOnUiThread(new Runnable()
public void run(){
{textview1.setText(String.valueOf(countInt))});
}
}
}, 1000);
It crashes because you are messing with something ( textview1.setText(countInt);) that belongs UI thread which is not allowed...

Categories

Resources