handler.removecallbacks无效

时间:2017-03-18 06:45:21

标签: android handler runnable

public class MainActivity extends AppCompatActivity  {
    int total = 0,k=0,j=0,i;
    public EditText editText;
    private int mInterval = 1000;
    Handler mHandler = new Handler();
    String formula_value,url_value;
    TextView textView,textView1;
    ArrayList<Integer> list;
    JSONArray m_jArry,m_jArry1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

        textView = (TextView)findViewById(R.id.textView);
        editText = (EditText) findViewById(R.id.editText);
        textView1 = (TextView) findViewById(R.id.val);
        //for focusing the edittext when clicked.
        editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    //Clear focus here from edittext
                    editText.clearFocus();
                }
                return false;
            }
        });
        try {
          //for focusing my JSON file no problem here.
            JSONObject obj = new JSONObject(loadJSONFromAsset());
            m_jArry1 = obj.getJSONArray("check");
            //calling the handler
            startRepeatingTask();

        }catch (JSONException e) {
            e.printStackTrace();
        } 
    }

OnDestroy()来销毁处理程序,这是我不知道该怎么做的地方

@Override
public void onDestroy() {
    super.onDestroy();
    mHandler.removeCallbacksAndMessages(null);
    stopRepeatingTask();
}

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

void stopRepeatingTask() {
    mHandler.removeCallbacks(mStatusChecker);
}

这是我的应用程序的核心

public void updateStatus(){
    try {
        JSONObject jo_inside = m_jArry.getJSONObject(list.get(k));
        k++;
        formula_value = jo_inside.getString("ques");
        url_value = jo_inside.getString("ans");
        textView.setText(formula_value);

我从JSON文件中获取问题并回答并检查edittext中给出的值是否与JSON文件中的答案值相同。         但是在解决这个停止处理程序问题之后我还应该看看另一个问题。当我在edittext中输入问题1的答案时,第一个循环结束并且编译器检查我用问题2的答案键入的答案我应该找到一种方法来减慢执行过程

        if (editText.getText().toString().equals(url_value)) {
            total++;
            textView1.setText("Correct");
        } else {
            textView1.setText("Wrong");
        }


        editText.setText(" ");
        if(k==6){
            k=0;
            j++;
        }
    }
    catch (JSONException e){
        e.printStackTrace();
    }
}

My Runnable:

Runnable mStatusChecker = new Runnable() {
    @Override
    public void run() {
        try {
            if(k==0){
                try {
                  //The handler should stop when j is equal to or greater than Array size which is 5 in this case but it keeps on looping.     

                    if(j>=m_jArry1.length()){
                        stopRepeatingTask();
                    }
                    JSONObject jsonObject = m_jArry1.getJSONObject(j);
                    m_jArry = jsonObject.getJSONArray("formules");
                    list = new ArrayList<Integer>(m_jArry.length());
                    for (i = 0; i < m_jArry.length(); i++) {
                        list.add(i);
                    }

                    Collections.shuffle(list);
                }catch(JSONException e){
                    e.printStackTrace();
                }
            }
            updateStatus();
        } finally {
            mHandler.postDelayed(mStatusChecker, mInterval);
        }
    }
};
 //I removed this loadJSONFromAsset() function because i have no problem in it.

}

2 个答案:

答案 0 :(得分:0)

  

OnDestroy()来销毁处理程序,这是我不知道该怎么做的地方   做

Sanjay将您的OnDestroy()函数更改为

@Override
public void onDestroy() {
    mHandler.removeCallbacksAndMessages(null);
    super.onDestroy();  // notice: we call super after removing handler callbacks and messages
}

答案 1 :(得分:0)

尝试将处理程序和可运行性设置为静态

 public static Handler handler = new Handler();
 public static Runnable runnable;

为我工作