android 启动计时器,如何在android中暂停并启动计时器?

您可以随时存储的timeleft这是S1和再次使用它像这样,阅读评论太

1)在呼叫计时器时,检查是否有任何存储时间

Page1.java

rowTextView.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);

long time = sp.getLong("time", 0); // get saved time of times

Intent myIntent = new Intent(v.getContext(),Page3.class);

myIntent.putExtra("time", time); // send it to page2

startActivity(myIntent);

finish();

}

});

2)使用时间如果它不是0,则为开始时间。

Page2.java

public class TimeractivitybestActivity extends Activity {

EditText e1;

MyCount counter;

Long s1;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

long time = this.getIntent().getLongExtra("time", 0); // get

// saved

// time

time = (time != 0) ? time : 1500;

e1 = (EditText) findViewById(R.id.editText1);

counter = new MyCount(time, 1000); // start with saved time

counter.start();

}

public void method(View v) {

switch (v.getId()) {

case R.id.button1:

counter.cancel();

break;

case R.id.button2:

counter = new MyCount(s1, 1000);

counter.start();

}

}

public class MyCount extends CountDownTimer {

public MyCount(long millisInFuture, long countDownInterval) {

super(millisInFuture, countDownInterval);

}

@Override

public void onFinish() {

e1.setText("DONE");

}

@Override

public void onTick(long millisUntilFinished) {

s1 = millisUntilFinished;

e1.setText("left:" + millisUntilFinished/1000);

}

}

public void onPause() {

SharedPreferences sp = PreferenceManager

.getDefaultSharedPreferences(this);

Editor et = sp.edit();

et.putLong("time", s1); // save time SharedPreference in onPause

et.commit();

}

}

3)我想在页面3中没有变化。

Page3.java

public void gobacktopage1(View v)

{

Intent myIntent = new Intent(v.getContext(),Page1.class);

startActivity(myIntent);

finish();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值