处理程序未按计划运行

时间:2017-03-16 06:45:58

标签: android timer handler postdelayed

// ==UserScript==
// @name         Reload Page If RegEx Match Not Found
// @namespace   
// @version      0.1
// @description  Reloads Page Until RegEx Match Found
// @author       TJ
// @match        https://system.fakepage.com/someotherpage
// @grant        none
// ==/UserScript==   
$(document).ready(function() {
    var regex = /PO\d{6}/g;
    var PO = document.body.innerHTML.match(regex);
    if (PO) {
        console.log("Found:",PO);
    } else {
        location.reload();
    }
});

这里我给了6秒..真正发生的是纯文本视图显示6秒然后最后一个问题出现在屏幕上..

try 
{              
          //got input from asset that's not a problem

           JSONObject obj = new JSONObject(loadJSONFromAsset());

            JSONArray m_jArry1 = obj.getJSONArray("check");
            int j=0,i,k=0;

            while(j<m_jArry1.length()) {

                JSONObject jsonObject = m_jArry1.getJSONObject(j);
                JSONArray m_jArry = jsonObject.getJSONArray("formules");

                ArrayList<Integer> list = new ArrayList<Integer>(m_jArry.length());
                for (i = 0; i < m_jArry.length(); i++) {
                    list.add(i);
                }

                Collections.shuffle(list);

              //shuffled the list for random generation of questions and answer 
              // so that's not a problem too.    


                for (i = 0; i < 6; i++) {

                    JSONObject jo_inside = m_jArry.getJSONObject(list.get(i));
                    formula_value = jo_inside.getString("ques");
                    url_value = jo_inside.getString("ans");



          /*I want to read question and answer from formula_value &
            url_value each time and wait for 6 seconds until the next set arrives... 
            So I used Handler to make it wait for 6 seconds which isn't working ..         
            but  it's going to the last value  directly .*/

                    handler = new Handler();
                    handler.postDelayed(new Runnable(){
                        @Override
                        public void run() {


                            textView.setText(formula_value);


                            editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {


                //using this so that there is an action only when done is pressed .
                                @Override
                                public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                                    if(actionId== EditorInfo.IME_ACTION_DONE){
                                        //Clear focus here from edittext
                                        editText.clearFocus();
                                    }
                                    if(editText.getText().equals(url_value)){
                                        total++;
                                        textView1.setText("Correct");
                             //display correct if right 
                                    }
                                    else{
                                        textView1.setText("Wrong");
                             //display wrong if wrong
                                    }

                                    return false;
                                }
                            });

                        }


                },6000);

查看日志:他们可以快速登录而无需等待。  请帮帮我吧 enter image description here

2 个答案:

答案 0 :(得分:0)

这是因为您没有在处理程序的runnable中更新您的值。 Handler.Postdelay不会暂停循环。循环正在执行。并且您的值在6秒之前更新。当您的6秒结束时,您将在循环执行时始终获得最后一个值。

我观察到的另一件事是你在这里使用嵌套循环。我建议你在这种情况下使用递归。感谢

答案 1 :(得分:0)

我发现问题谢谢了..我正在使用for循环,这是一个很大的错误。我现在对如何停止处理程序有很大疑问。我用了

    mHandler.removeCallbacksAndMessages(null);

                or

    mHandler.removeCallbacks(runnable);

并做了所有可能的步骤来停止这个过程。什么都没有,有一个无限循环执行。我不知道如何阻止它:(

C这里是兄弟:handler.removecallbacks not working