[英]How do I stop a runnable when I start another activity?
I have a Android app that shows a dialog when a value is '1' and shows the dialog repeatedly till the value is set to '0'. 我有一个Android应用程序,当值是“ 1”时会显示一个对话框,并反复显示该对话框,直到该值设置为“ 0”为止。 A Runnable
calls a Handler
which starts the dialog, and the Runnable
loops with a delay. Runnable
调用Handler
来启动对话框,并且Runnable
延迟循环。
Problem is that when I go to another activity with the same function and go back, the dialog is already open. 问题是,当我转到具有相同功能的另一个活动并返回时,该对话框已经打开。 This causes my app to crash. 这导致我的应用崩溃。 I already try to use removeMessage and removeCallback but still have the problem. 我已经尝试使用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);
}
Error 错误
/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)
Based on the documentation for removeCallbacksAndMessages
基于removeCallbacksAndMessages
的文档
Remove any pending posts of callbacks and sent messages whose obj is token . 删除obj为token的回调和已发送消息的所有未决帖子。 If token is null, all callbacks and messages will be removed 如果token为null,则将删除所有回调和消息
Your code won't match any token: myHandler.removeCallbacksAndMessages(aMyRunnable);
您的代码将不匹配任何令牌: myHandler.removeCallbacksAndMessages(aMyRunnable);
So the solution would be to use: 因此解决方案是使用:
myHandler.removeCallbacksAndMessages(null);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.