调用removeCallbacksAndMessages(null)后处理程序不会结束

时间:2019-03-05 10:42:37

标签: java android handler

我有两个处理程序。处理程序中的处理程序。他们两个都在for循环中。

概述是这样的,

for{
    handler.postDelayed(runnableA{
        for{
            handler2.postDelayed(runnableB{
                function();
            }, 3000);
        }
    }, 1000);
}

我想在用户单击back button时随时结束处理程序的工作。因此,我创建了两个Runnable类,以便可以使用类似runnableA.removellbacksAndMessages(null)的东西。

Handler messageHandler;
Handler countDownHandler;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Toast.makeText(this, "Start Play in 5 seconds", Toast.LENGTH_SHORT).show();

    countDownHandler = new Handler();

    for (int i = 7; i >= 0; --i) {
        final int idx = i;
        Runnable countRunnable = new CountRunnable(idx, countDownView);
        countDownHandler.postDelayed(countRunnable, 1000 * (7 - i));
    }
}

这是可运行类。

public class CountRunnable implements Runnable {
    int idx;
    TextView countDownView;

    public CountRunnable(int idx, TextView countDownView) {
        this.idx = idx;
        this.countDownView = countDownView;
    }

    @Override
    public void run() {
        int messageSize = messageItems.size();
        for (int j = 0; j < messageSize; j++) {
            final int jdx = j;
            messageHandler = new Handler();
            Runnable messageRunnable = new MessageRunnable(jdx);
            messageHandler.postDelayed(messageRunnable, 3000 * jdx);
        }
    }
}

class MessageRunnable implements Runnable {
    int jdx;

    public MessageRunnable(int jdx) {
        this.jdx = jdx;
    }

    @Override
    public void run() {
        addMessageView(messageItems.get(jdx));
    }
}

这是onBackPressed()

@Override
public void onBackPressed() {
    super.onBackPressed();
    Toast.makeText(getApplicationContext(), "All Work Ended.", Toast.LENGTH_SHORT).show();
    scrollFlag = true;

    try {
        messageHandler.removeCallbacksAndMessages(null);
    } catch (Exception e) {
        Log.d(TAG, "messageHandler never used");
        e.printStackTrace();
    }
    try {
        countDownHandler.removeCallbacksAndMessages(null);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
public void addMessageView(String message){
    try{
        mTextView.setText(message);
    }catch(Exception e){
        Toast.makeText(getApplicationContext(), "Abnormal End", Toast.LENGTH_SHORT).show();
    }
}

但是,由于活动已经结束,但处理程序找不到活动,因此我不断收到错误消息。因此,Abnormal End的Toast消息显示的内容与内部for循环的大小一样。

如果我不使用Toast消息,则可以忽略它,但是我担心内存泄漏或格式错误的程序之类的东西。

如何解决此问题?

1 个答案:

答案 0 :(得分:1)

主要问题是您要创建nCountRunnable号和mMessageRunnables号。尽管创建了多个处理程序,但您仅删除了最新创建的Hanlder的回调。

这是您应该做的:

保留所有HandlerRunnables的引用,并对其全部调用messageHandler.removeCallbacksAndMessages(null);countDownHandler.removeCallbacksAndMessages(null);