How To Pause And Start The Timer In Android?
I am working on android applications. In my project I have 3 pages. The first page consists of 1 button. The second page is consists of the timer code. The third page consis
Solution 1:
You can always store the timeLeft which is s1 and use it again like this, Read the comments too
1) While calling timer,check if you have any stored time
Page1.java
rowTextView.setOnClickListener(newOnClickListener() {
publicvoidonClick(View v) {
SharedPreferencessp= PreferenceManager.getDefaultSharedPreferences(this);
longtime= sp.getLong("time", 0); // get saved time of timesIntentmyIntent=newIntent(v.getContext(),Page3.class);
myIntent.putExtra("time", time); // send it to page2
startActivity(myIntent);
finish();
}
});
2) Use the time to start time if it's not 0.
Page2.java
publicclassTimeractivitybestActivityextendsActivity {
EditText e1;
MyCount counter;
Long s1;
@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
longtime=this.getIntent().getLongExtra("time", 0); // get// saved// time
time = (time != 0) ? time : 1500;
e1 = (EditText) findViewById(R.id.editText1);
counter = newMyCount(time, 1000); // start with saved time
counter.start();
}
publicvoidmethod(View v) {
switch (v.getId()) {
case R.id.button1:
counter.cancel();
break;
case R.id.button2:
counter = newMyCount(s1, 1000);
counter.start();
}
}
publicclassMyCountextendsCountDownTimer {
publicMyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@OverridepublicvoidonFinish() {
e1.setText("DONE");
}
@OverridepublicvoidonTick(long millisUntilFinished) {
s1 = millisUntilFinished;
e1.setText("left:" + millisUntilFinished / 1000);
}
}
publicvoidonPause() {
SharedPreferencessp= PreferenceManager
.getDefaultSharedPreferences(this);
Editoret= sp.edit();
et.putLong("time", s1); // save time SharedPreference in onPause
et.commit();
}
}
3) no change in page 3, I suppose.
Page3.java
publicvoidgobacktopage1(View v){
Intent myIntent = newIntent(v.getContext(),Page1.class);
startActivity(myIntent);
finish();
}
Solution 2:
publicclassTimerActivityextendsActivity{
EditText e1;
MyCount counter;
Long s1;
/** Called when the activity is first created. */@OverridepublicvoidonCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
e1=(EditText)findViewById(R.id.editText1);
counter= newMyCount(5000,1000);
counter.start();
}
publicvoidasdf(View v)
{
switch(v.getId())
{
case R.id.button1: counter.cancel();
break;
case R.id.button2: counter= newMyCount(s1,1000);
counter.start();
}
}
publicclassMyCountextendsCountDownTimer
{
publicMyCount(long millisInFuture, long countDownInterval)
{
super(millisInFuture, countDownInterval);
}
@OverridepublicvoidonFinish()
{
e1.setText("DONE");
}
@OverridepublicvoidonTick(long millisUntilFinished)
{
s1=millisUntilFinished;
e1.setText("left:"+millisUntilFinished/1000);
}
}
}
Solution 3:
this is how it works...
MyCount counter;
Long s1;
counter= newMyCount(300000,1000);
counter.start();
publicvoidasdf(View v){ <---- method for onclick of buttons pause and resuming timer
switch(v.getId()){
case R.id.button1:<-- for pause
counter.cancel();
break;
case R.id.button2:<--- for resume
counter= newMyCount(s1,1000);
counter.start();
}
}
publicclassMyCountextendsCountDownTimer{
publicMyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@OverridepublicvoidonFinish() {
mediaplayer.stop();
mediaplayer.release();
}
@OverridepublicvoidonTick(long millisUntilFinished) {
s1=millisUntilFinished;
}
}
case R.id.button1:<-- for pause
counter.cancel();
this the one which is used to pause the timer and start again...
and in ur case
public void gobacktopage1(View v) { Intent myIntent = new Intent(v.getContext(),Page1.class); startActivity(myIntent); finish(); }
write a method in that add counter.cancel(); in that method and call that method...
Post a Comment for "How To Pause And Start The Timer In Android?"