停止处理程序. postDelayed()

我通过 new Handler () . postDelayed (new Runnable () ... 调用多个 Handler。 我怎样才能停止时,我点击回来?

public class MyActivity extends AppCompatActivity implements OnClickListener {


private Button btn;
private Handler handler;
private Runnable myRunnable;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
btn = (Button) findViewById(R.id.trainingsstart);
btn.setOnClickListener(this);
}


@Override
public void onClick(View v) {
Handler handler =  new Handler();
Runnable myRunnable = new Runnable() {
public void run() {
// Things to be done
}
};


handler.postDelayed(myRunnable, 3000);
}


@Override
public void onBackPressed() {
super.onBackPressed();
handler.removeCallbacks(myRunnable);
}
}

我有一个问题,我的处理程序没有一个像“ myHandler”这样的名称。我该如何处理它?

谢谢帮忙!

现在我得到了这些错误:

03-28 17:47:03.662: E/AndroidRuntime(1961): FATAL EXCEPTION: main
03-28 17:47:03.662: E/AndroidRuntime(1961): java.lang.NullPointerException
03-28 17:47:03.662: E/AndroidRuntime(1961):     at de.basti12354.tage.uebungen.Tag1.onBackPressed(Tag1.java:120)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.app.Activity.onKeyUp(Activity.java:2159)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.view.KeyEvent.dispatch(KeyEvent.java:2647)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.app.Activity.dispatchKeyEvent(Activity.java:2389)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1860)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:3791)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3774)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3429)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3398)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3483)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3406)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3540)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3429)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3398)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3406)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3429)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3398)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3516)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:3666)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:1982)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:1698)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:1689)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFinished(InputMethodManager.java:1959)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.os.MessageQueue.nativePollOnce(Native Method)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.os.MessageQueue.next(MessageQueue.java:132)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.os.Looper.loop(Looper.java:124)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.app.ActivityThread.main(ActivityThread.java:5103)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at java.lang.reflect.Method.invokeNative(Native Method)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at java.lang.reflect.Method.invoke(Method.java:525)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at dalvik.system.NativeStart.main(Native Method)
138588 次浏览

你可使用:

 Handler handler = new Handler()
handler.postDelayed(new Runnable())

或者你可以使用:

 handler.removeCallbacksAndMessages(null);

医生

Public final void RemoveCallbacksAndMessages (对象标记)

在 API 级别1中添加删除任何回调挂起的帖子并发送 消息的 obj 是令牌。如果令牌为空,则所有回调和 信息将被删除。

或者你也可以这样做:

Handler handler =  new Handler()
Runnable myRunnable = new Runnable() {
public void run() {
// do something
}
};
handler.postDelayed(myRunnable,zeit_dauer2);

然后:

handler.removeCallbacks(myRunnable);

医生

Public final void RemoveCallback (Runnable r)

在 API 级别1中添加删除任何悬而未决的 Runnable r 文章 在消息队列中。

Public final void RemoveCallback (可运行 r,对象标记)

编辑:

改变这一点:

@Override
public void onClick(View v) {
Handler handler =  new Handler();
Runnable myRunnable = new Runnable() {

致:

@Override
public void onClick(View v) {
handler = new Handler();
myRunnable = new Runnable() { /* ... */}

因为你有以下几点。在 onCreate之前声明,但是您重新声明,然后在 onClick中初始化它,导致 NPE。

Handler handler; // declared before onCreate
Runnable myRunnable;

这可能是旧的,但对于那些寻找答案,你可以使用这个..。

public void stopHandler() {
handler.removeMessages(0);
}

干杯

您可以定义一个布尔值,当您想停止处理程序时,可以将其更改为 false。

boolean stop = false;


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


//do your work here..


if (!stop) {
handler.postDelayed(this, delay);
}
}
}, delay);
  Boolean condition=false;  //Instance variable declaration.


//-----------------Inside oncreate---------------------------------------------------
start =(Button)findViewById(R.id.id_start);
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {


starthandler();


if(condition=true)
{
condition=false;
}




}
});


stop=(Button) findViewById(R.id.id_stoplocatingsmartplug);


stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
stophandler();


}
});




}


//-----------------Inside oncreate---------------------------------------------------


public void starthandler()
{


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




if(!condition)
{
//Do something after 100ms




}


}
}, 5000);


}




public void stophandler()
{
condition=true;
}

说明:

必须在“ onCreate”之前声明特定的处理程序。如果它是在‘ onCreate’中声明的,则它们不共享相同的声明,因此无法识别。这就是作者面临的问题。他不小心在‘ onCreate’中声明了特定的处理程序。

解决方案:

public class MyActivity extends AppCompatActivity implements OnClickListener {


private Button btn;
private Handler handler;
private Runnable myRunnable;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
btn = (Button) findViewById(R.id.trainingsstart);
btn.setOnClickListener(this);
}


@Override
public void onClick(View v) {
handler =  new Handler();
myRunnable = new Runnable() {
public void run() {
// Things to be done
}
};


handler.postDelayed(myRunnable, 3000);
}


@Override
public void onBackPressed() {
super.onBackPressed();
handler.removeMessages(0);
}
}

最好的选择是使用布尔值作为标志,在 onPause & onResume 中使用布尔值

TIME_OUT = 45000;


//add this boolean
boolean run =true;


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


//run this method only when run is true
if(run==true){


//your code
}


}
}, TIME_OUT);


//change values in onPause
run=false;