Stop Handler.Postdelayed()

Stop handler.postDelayed()

You can use:

 Handler handler = new Handler()
handler.postDelayed(new Runnable())

Or you can use:

 handler.removeCallbacksAndMessages(null);

Docs

public final void removeCallbacksAndMessages (Object token)

Added in API level 1 Remove any pending posts of callbacks and sent
messages whose obj is token. If token is null, all callbacks and
messages will be removed.

Or you could also do like the following:

Handler handler =  new Handler()
Runnable myRunnable = new Runnable() {
public void run() {
// do something
}
};
handler.postDelayed(myRunnable,zeit_dauer2);

Then:

handler.removeCallbacks(myRunnable);

Docs

public final void removeCallbacks (Runnable r)

Added in API level 1 Remove any pending posts of Runnable r that are
in the message queue.

public final void removeCallbacks (Runnable r, Object token)

Edit:

Change this:

@Override
public void onClick(View v) {
Handler handler = new Handler();
Runnable myRunnable = new Runnable() {

To:

@Override
public void onClick(View v) {
handler = new Handler();
myRunnable = new Runnable() { /* ... */}

Because you have the below. Declared before onCreate but you re-declared and then initialized it in onClick leading to a NPE.

Handler handler; // declared before onCreate
Runnable myRunnable;

Android Studio - How to stop handler.postDelayed from a fragment?

You can just skip method call if fragment's view is destroyed (fragment destroyed or it is in backstack). There is getView() method

handler.postDelayed(new Runnable() {
@Override
public void run() {
if(getView() != null){
fillRecycler2();
}
}
},1500);

If you use hide/show methods of FragmentTransaction you can use isVisible() method of Fragment, because hidden fragments view is View.GONE (not null)

How to Stop Handler thread?

Handler handler = new Handler();
Runnable runnable = new Runnable() {
@Override
public void run() {
Intent i = new
Intent(MapsActivity.this,MapsActivity.class);
startActivity(i);
finish();
}
};
handler.post(runnable);

// use this when you don't want callback to be called anymore
handler.removeCallbacks(runnable);

How to stop Handler in Android

You can use this to stop that runnable

handler.removeCallbacks(calendarUpdater);

removeCallbacks(Runnable r) :Remove any pending posts of Runnable r that are in the message queue.

Edit

You can organize your code like this

In your onCreate() of MainActivity.java

Handler handler = new Handler();
refreshCalendar()

//outside oncreate

public void refreshCalendar() {
calAdapter.refreshDays();
calAdapter.notifyDataSetChanged();
startRepeatingTask();
calTitle.setText(android.text.format.DateFormat.format("MMMM yyyy", cal));
}

public Runnable calendarUpdater = new Runnable() {

@Override
public void run() {
items.clear();
allData = new ArrayList<HashMap<String,String>>();
allData.clear();
allData = db.showAllEvents();

String currentDate = (String)android.text.format.DateFormat.format("MM/yyyy", cal);
for(int i=0; i<allData.size(); i++)
{
String date[] = allData.get(i).get("date").split("/");
String md[] = currentDate.split("/");
if(date[1].equals(md[0]) && date[2].equals(md[1]))
{
items.add(date[0]);
System.out.println("dates: "+date[0]);
}
}
calAdapter.setItems(items);
calAdapter.notifyDataSetChanged();
handler.postDelayed(calendarUpdater,5000); // 5 seconds
}
};

void startRepeatingTask()
{
calendarUpdater.run();
}

void stopRepeatingTask()
{
handler.removeCallbacks(calendarUpdater);
}

Now you can just call startRepeatingTask() to posting message and to stop use stopRepeatingTask()

Inherited from following link

Repeat a task with a time delay?

How can I stop Handler in postDelayed mode?

Use removeCallbacks

nHandler.removeCallbacks(nhandlerTask);

http://developer.android.com/reference/android/os/Handler.html#removeCallbacks(java.lang.Runnable)

public final void removeCallbacks (Runnable r)

Added in API level 1
Remove any pending posts of Runnable r that are in the message queue.

How to stop handler after certain time period (In my case 30 sec)?

1) delay is in milliseconds, if you want to change image every second handler.postDelayed(this,100); should be changed to handler.postDelayed(this,1000); (1s = 1000ms)

2) stop task:

    long startTime = System.currentTimeMillis();
final Handler handler = new Handler();
Runnable runnable = new Runnable() {
int i = 0;
@Override
public void run() {
imageView.setImageResource(imageArray[i]);
i++;
if(i>imageArray.length-1){
i= 0;
}
if (System.currentTimeMillis() - startTime < 30000){
handler.postDelayed(this,1000);
} else {
//TODO start second activity
}
}
};

or you can schedule second runnable to stop changing images after 30s and start another activity:

    handler.postDelayed(new Runnable() {
@Override
public void run() {
handler.removeCallbacks(runnable); //stop next runnable execution

//TODO start second activity
}
}, 30000);

cancelling a handler.postdelayed process

I do this to post a delayed runnable:

myHandler.postDelayed(myRunnable, SPLASH_DISPLAY_LENGTH); 

And this to remove it: myHandler.removeCallbacks(myRunnable);

How to cancel handler.postDelayed?

I do this to cancel postDelays, per the Android: removeCallbacks removes any pending posts of Runnable r that are in the message queue.

handler.removeCallbacks(runnableRunner);

or use to remove all messages and callbacks

handler.removeCallbacksAndMessages(null);

handler.postDelayed() not stopping

Right now, you call stopRepeatingTask() on reaching a certain limit (progress == 90). But in the finally block, you unconditionally start the next task. You should only start the new task if the limit has not yet been reached:

Runnable mStatusChecker = new Runnable() {
@Override
public void run() {
try {
Log.d( "","entered run ");
mWaveLoadingView.setCenterTitle(String.valueOf(progress)+"%");
mWaveLoadingView.setProgressValue(progress);
progress+=1;
if(progress==90)
stopRepeatingTask();

} finally {
// 100% guarantee that this always happens, even if
// your update method throws an exception

// only if limit has not been reached:
if(progress<90){
mHandler.postDelayed(mStatusChecker, mInterval);
}
}
}
};


Related Topics



Leave a reply



Submit