当我开始另一项活动时,如何停止可运行?

时间:2018-04-19 21:50:21

标签: java android android-activity handler runnable

我有一个Android应用程序,当值为“1”时会显示一个对话框,并反复显示该对话框,直到该值设置为“0”。 Runnable调用Handler启动对话框,Runnable调用延迟。

问题是当我转到具有相同功能的另一个活动并返回时,对话框已经打开。这会导致我的应用崩溃。我已经尝试使用removeMessage和removeCallback但仍然有问题。

Handler

Handler myHandler = new Handler()
{
    @Override
    public void handleMessage(Message msg)
    {
        /* Dialog */
        final AlertDialog.Builder AlertAlarm_Build;
        LayoutInflater inflater = (LayoutInflater) Settings_Activity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        AlertAlarm_Build = new AlertDialog.Builder(Settings_Activity.this);
        final View Disengaged_View;
        Disengaged_View = inflater.inflate(R.layout.disengage_dialog,null);
        final AlertDialog PasswordDialog = AlertAlarm_Build.create();
        PasswordDialog.show();

        final String[] given_password = new String[1];
        final boolean[] Password_Pass = {false};

        //respose
        final RequestQueue requestHander;
        requestHander = (RequestQueue) Volley.newRequestQueue(getApplicationContext());

...//Ask for password

        //New
        PasswordDialog.setOnDismissListener(new DialogInterface.OnDismissListener()
        {
            @Override
            public void onDismiss(DialogInterface dialogInterface)
            {
                recreate();
            }
        });

    }

};

Runnable

    //Runnable
final Runnable aMyRunnable = new Runnable()
{
    @Override
    public void run()
    {
        RequestQueue requestRun;
        requestRun = (RequestQueue) Volley.newRequestQueue(getApplicationContext());

        if(New_engaged[0].equals("1") && New_alarm[0].equals("1"))
        {
            set_engaged[0] = "1";
            myHandler.sendEmptyMessage(0);
        }
        else
        {
            requestRun.add(JOR_SystemCheck);
            myHandler.postDelayed(this,5000);

        }
    }
};

onStop

    protected void onStop() {
        super.onStop();
        myHandler.removeCallbacksAndMessages(aMyRunnable);
        myHandler.removeMessages(0);
    }

错误

/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.djwiz.eclipse5, PID: 17705
              android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@f8866f3 is not valid; is your activity running?
                  at android.view.ViewRootImpl.setView(ViewRootImpl.java:765)
                  at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:356)
                  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:92)
                  at android.app.Dialog.show(Dialog.java:330)
                  at com.example.djwiz.eclipse5.Settings_Activity$1.handleMessage(Settings_Activity.java:68)
                  at android.os.Handler.dispatchMessage(Handler.java:105)
                  at android.os.Looper.loop(Looper.java:164)
                  at android.app.ActivityThread.main(ActivityThread.java:6541)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

1 个答案:

答案 0 :(得分:0)

Based on the documentation for removeCallbacksAndMessages

Remove any pending posts of callbacks and sent messages whose obj is token. If token is null, all callbacks and messages will be removed

Your code won't match any token: myHandler.removeCallbacksAndMessages(aMyRunnable);

So the solution would be to use:

myHandler.removeCallbacksAndMessages(null);

相关问题