How to pause and start the timer in android?

2019-09-02 08:11发布

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 consists of again a button.

Now my requirement is when I click on the first page button the third page should open and the timer in second page should pause. Again when I click on the third page button the second page timer should restart the time where it is stopped and should open the first page.

I am struggling to achieve this task.Guide me through it, Suggest what should have been done to do that.

Page1.java

rowTextView.setOnClickListener(new OnClickListener() {
  public void onClick(View v) {

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

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);
        e1 = (EditText) findViewById(R.id.editText1);
        counter = new MyCount(15000, 1000);
        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);
        }
    }
}


Page3.java

public void gobacktopage1(View v)
    {
        Intent myIntent = new Intent(v.getContext(),Page1.class);
        startActivity(myIntent);
        finish();
    }

3条回答
等我变得足够好
2楼-- · 2019-09-02 08:46
public class TimerActivity extends Activity{

EditText e1;
MyCount counter;
Long s1;
/** Called when the activity is first created. */ 
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    e1=(EditText)findViewById(R.id.editText1);
    counter= new MyCount(5000,1000);
    counter.start();
}
public void asdf(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);
    }
}

}

查看更多
Root(大扎)
3楼-- · 2019-09-02 08:49

this is how it works...

MyCount counter;
Long s1;

     counter= new MyCount(300000,1000);
    counter.start();

public void asdf(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= new MyCount(s1,1000);
         counter.start();
    }
}
public class MyCount extends CountDownTimer{
    public MyCount(long millisInFuture, long countDownInterval) {
    super(millisInFuture, countDownInterval);
    }
    @Override
    public void onFinish() {
    mediaplayer.stop();
   mediaplayer.release();
    }
    @Override
    public void onTick(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...

查看更多
▲ chillily
4楼-- · 2019-09-02 08:52

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(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) Use the time to start time if it's not 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) no change in page 3, I suppose.

    Page3.java 

    public void gobacktopage1(View v) 
        { 
            Intent myIntent = new Intent(v.getContext(),Page1.class); 
            startActivity(myIntent); 
            finish(); 
        } 
查看更多
登录 后发表回答