0

我想CountDownTimer从另一个活动开始。我使用处理程序来做到这一点,但它不起作用。我做错了哪一部分?这是我的代码:

activity1.java

public class Activity1 extends Activity {
public static Handler mHandler; 

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initHandler();
mHandler.sendEmptyMessage(1);

startActivity(new Intent(Activity1.this, Activity2.class));
}

private void initHandler(){
mHandler = new Handler(){
    @Override
    public void handleMessage(Message msg) {
        switch (msg.arg1) {
        case 1:
            mCountDownTimer.start();
            break;
        }
    }
};
}

private CountDownTimer mCountDownTimer = new CountDownTimer(10000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
    Toast.makeText(Activity1.this, "Count is: "+ millisUntilFinished/1000,    Toast.LENGTH_SHORT).show();
}

@Override
public void onFinish() {
    // TODO Auto-generated method stub

}
};
}

activity2.java

public class Activity2 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

Activity1.mHandler.sendEmptyMessage(1);

}
}

为什么这不起作用?

4

1 回答 1

1

在您的initHandler()方法中,将您的情况switch更改switch (msg.arg1)switch (msg.what)

根据Android 开发者网站 sendEmptyMessage(int) Sends a Message containing only the what value.

于 2013-04-29T07:43:29.237 回答