我想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);
}
}
为什么这不起作用?