Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/195.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Handler没有';单击按钮时不要停止_Java_Android_Handler - Fatal编程技术网

Java Handler没有';单击按钮时不要停止

Java Handler没有';单击按钮时不要停止,java,android,handler,Java,Android,Handler,我有一个启动活动的runnable和一个让runnable在5秒后重复的处理程序。然而,我实现了一个“停止按钮”,它应该停止该处理程序,但不知何故它不起作用。 我希望能得到一些帮助,为什么它不能停止循环 public class MainActivity extends Activity { private Button callBtn; private Button stopBtn; Handler handler = new Handler(); Runn

我有一个启动活动的runnable和一个让runnable在5秒后重复的处理程序。然而,我实现了一个“停止按钮”,它应该停止该处理程序,但不知何故它不起作用。 我希望能得到一些帮助,为什么它不能停止循环

public class MainActivity extends Activity {

    private Button callBtn;
    private Button stopBtn;

    Handler handler = new Handler();
    Runnable redial=new Runnable(){

    @Override public void run(){Intent callIntent=new Intent(Intent.ACTION_CALL,Uri.parse("tel:+49888888"));startActivity(callIntent);

    }};

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        callBtn = (Button) findViewById(R.id.call);
        stopBtn = (Button) findViewById(R.id.stop);

        // add PhoneStateListener for monitoring
        MyPhoneListener phoneListener = new MyPhoneListener();
        TelephonyManager telephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
        // receive notifications of telephony state changes
        telephonyManager.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);

        callBtn.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                redial.run();
            }
        });

        stopBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                handler.removeCallbacks(redial);
            }
        });

    }

    private class MyPhoneListener extends PhoneStateListener {

        private boolean onCall = false;


        public void onCallStateChanged(int state, String incomingNumber) {

            switch (state) {
                case TelephonyManager.CALL_STATE_RINGING:
                    // phone ringing...
                    Toast.makeText(MainActivity.this, incomingNumber + " calls you",
                            Toast.LENGTH_LONG).show();
                    break;

                case TelephonyManager.CALL_STATE_OFFHOOK:
                    // one call exists that is dialing, active, or on hold
                    Toast.makeText(MainActivity.this, "on call...",
                            Toast.LENGTH_LONG).show();
                    //because user answers the incoming call
                    onCall = true;
                    break;

                case TelephonyManager.CALL_STATE_IDLE:
                    // in initialization of the class and at the end of phone call

                    // detect flag from CALL_STATE_OFFHOOK
                    if (onCall == true) {
                        Toast.makeText(MainActivity.this, "restart app after call",
                                Toast.LENGTH_LONG).show();

                        // restart our application
                        Intent restart = getBaseContext().getPackageManager().
                                getLaunchIntentForPackage(getBaseContext().getPackageName());
                        restart.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(restart);

                        onCall = false;

                        handler.postDelayed(redial, 5000);


                    }
                    break;
                default:
                    break;
            }

        }

    }

简单地调用redial.run()将只运行该方法一次,而不运行其他方法。要实际使用Runnable,您需要使用处理程序类的post()方法之一。您没有正确使用处理程序。您应该调用handler.post到runnable以删除回调。简单调用redial.run()将只运行该方法一次,而不运行其他方法。要实际使用Runnable,您需要使用处理程序类的post()方法之一。您没有正确使用处理程序。您应该调用handler.post到runnable以删除回调。