简体   繁体   中英

How Can I Stop Runnable Into 2 different Acivity with in Android?

I need to delete a value from SharedPreferences after 5 minutes or when the user finished to do something . So when I add that value I start this:

Activity A

Handler handler = new Handler();
                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            mySharedPreferences.removeValue(mContext, Utils.MY_VALUE);
                        }
                    }, Utils.TIME_BEFORE_DELETE);

and in the case users finished all I do this:

Activity B

mySharedPrefernces.removeValue(mContext, Utils.MY_VALUE);

But how can I stop the Handle into second activity ?? Or is there another way to do it??

you can you boolean variable if you want to cancel this.

create public static boolean to check if the task is cancelled or not.

public static boolean isCanceled = false;

Use this in run() method

Handler handler = new Handler();
handler.postDelayed(new Runnable() {
    @Override
    public void run() {
        if (!isCanceled)
                mySharedPreferences.removeValue(mContext, Utils.MY_VALUE);
        }
    }, Utils.TIME_BEFORE_DELETE);

if you want to cancel then set:

isCanceled = true;
Runnable run = new Runnable() {
  @Override
  public void run() {
    mySharedPreferences.removeValue(mContext, Utils.MY_VALUE);
  }
};
Handler handler = new Handler();
handler.postDelayed(run, Utils.TIME_BEFORE_DELETE);
//to dismiss pending runnable
handler.removeCallbacks(run);

You can use handler.removeCallbacksAndMessages(null); . More information link

In this case you can use service with sticky flags. So you start service with intent "start_handler" and start handler also. When you need cancel handler you send the intent to stop handler and service. Or when time is passed and handler calls your code you should also stop service. Using service with sticky flag provides possibility restoring handler. Also you need add some logic saving time when handler was run for correct restoring handler.

A better way to do: Example code

publc static final Handler handler = new Handler();
public static final Runnable runnable = new Runnable() {
     public void run() {
        try {
            Log.d("Runnable","Handler is working");
           if(i == 5){ // just remove call backs
                handler.removeCallbacks(this); 
                Log.d("Runnable","ok");
            } else { // post again
                i++;
                handler.postDelayed(this, 5000); 
            }
        } catch (Exception e) { 
            e.printStackTrace();
        }   
   }
};

//now somewhere in a method
b1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
    handler.removeCallbacks(runnable); 
    handler.postDelayed(runnable, 5000); 
}
});

For that you can't use direct Runnable inside handler, you need to take one instance of it then you can do this like below,

 Runnable myRunnable = new Runnable(){};

Then assign this in handler

 handler.postDelayed(myRunnable);

And on no need use below line

 handler.removeCallbacks(myRunnable);
new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            //add your code hare
            finish();
        }
    }, 10000);

by using this way you can stop your runnable in a fix time

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM