处理程序handleMessage未被调用

时间:2018-12-31 05:26:58

标签: android handler

我正在运行一个简单的处理程序,即使我执行了handler.sendMessage(),也不会调用handleMessage方法。

这是处理程序定义:

protected static class TimeoutHandler extends Handler {
    private final WeakReference<PROQuestion> activity;

    public TimeoutHandler(PROQuestion activity) {
        this.activity = new WeakReference<PROQuestion>(activity);
    }

    @Override
    public void handleMessage(Message msg) { //never gets called
        boolean firstScreen = true;
        if (activity.get() == null) {
            removeCallbacksAndMessages(0);
        } else {
            Intent startNewActivityOpen = null;  
            startNewActivityOpen = new Intent(activity.get(), Home.class);

            startNewActivityOpen.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            Bundle bundle = activity.get().getIntent().getExtras();
            if (bundle != null) startNewActivityOpen.putExtras(bundle);
            activity.get().startActivity(startNewActivityOpen);

            activity.get().finish();
   }
 }

我在onCreate()的主线程上调用处理程序:

@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   TimeoutHandler handlerTimeout = new TimeoutHandler(this);

   if (handlerTimeout != null) {
        removeTimeout();
        Message message = handlerTimeout.obtainMessage();
        message.what = 100;
        handlerTimeout.sendMessage(message);
        //handlerTimeout.sendMessageAtTime(message, SystemClock.uptimeMillis() + GlobalVars.longTimeout);
    }
    if (handlerTimeout.hasMessages(100)) { //returns true
        Log.d(getClass().getSimpleName(),"messages found");
    }
 }

为什么从未调用handleMessage方法?没有错误消息或崩溃。

0 个答案:

没有答案