如何暂停,并在安卓启动定时器? [英] How to pause and start the timer in android?
问题描述
我的工作在Android应用程序。在项目中,我有3个页面。结果
- 的第一页由1个按钮。
- 的第二页是由定时器code的。
- 第三页再次由一个按钮。
现在我的要求是,当我点击第一个按钮页第三页应该打开,并在第二页的计时器应该暂停。当我再次点击第三页按钮
第二页的计时器应处重新开始停止的时间,并应打开第一页。
我在努力实现这一task.Guide我通过它,有什么建议应该已经完成,以做到这一点。
Page1.javarowTextView.setOnClickListener(新OnClickListener(){
公共无效的onClick(视图v){ 意图myIntent =新意图(v.getContext(),Page3.class);
startActivity(myIntent);
完();
}
});Page2.java公共类TimeractivitybestActivity延伸活动{
E1的EditText;
mycount的计数器;
龙S1; @覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
E1 =(EditText上)findViewById(R.id.editText1);
计数器=新mycount的(15000,1000);
counter.start();
} 公共无效方法(视图v){ 开关(v.getId()){ 案例R.id.button1:
counter.cancel();
打破;
案例R.id.button2:
计数器=新mycount的(S1,1000);
counter.start();
}
} 公共类mycount的扩展CountDownTimer {
公共mycount的(长millisInFuture,长countDownInterval){
超(millisInFuture,countDownInterval);
}
@覆盖
公共无效onFinish(){
e1.setText(DONE);
}
@覆盖
公共无效onTick(长millisUntilFinished){
S1 = millisUntilFinished;
e1.setText(左+ millisUntilFinished / 1000);
}
}
}
Page3.java公共无效gobacktopage1(视图V)
{
意图myIntent =新意图(v.getContext(),Page1.class);
startActivity(myIntent);
完();
}
您可以随时存储的的timeleft这是S1 的,并再次使用它像这样,的阅读评论太
1)在呼吁计时器,检查是否有任何存储的时间
Page1.java
rowTextView.setOnClickListener(新OnClickListener(){
公共无效的onClick(视图v){
共享preferences SP = preferenceManager.getDefaultShared preferences(本);
很长一段时间= sp.getLong(时间,0); //获取的时间节省了时间
意图myIntent =新意图(v.getContext(),Page3.class);
myIntent.putExtra(时间,时间); //发送到第2页
startActivity(myIntent);
完();
}
});
2)使用的时候,如果它不是0开始的时间。
Page2.java 公共类TimeractivitybestActivity延伸活动{
E1的EditText;
mycount的计数器;
龙S1; @覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
很长一段时间= this.getIntent()getLongExtra(时间,0); //获取
//保存
// 时间
时间=(时间!= 0)?时间:1500;
E1 =(EditText上)findViewById(R.id.editText1);
计数器=新mycount的(时间,1000); //用节省的时间开始
counter.start();
} 公共无效方法(视图v){ 开关(v.getId()){ 案例R.id.button1:
counter.cancel();
打破;
案例R.id.button2:
计数器=新mycount的(S1,1000);
counter.start();
}
} 公共类mycount的扩展CountDownTimer {
公共mycount的(长millisInFuture,长countDownInterval){
超(millisInFuture,countDownInterval);
} @覆盖
公共无效onFinish(){
e1.setText(DONE);
} @覆盖
公共无效onTick(长millisUntilFinished){
S1 = millisUntilFinished;
e1.setText(左+ millisUntilFinished / 1000);
}
} 公共无效的onPause(){
共享preferences SP = preferenceManager
.getDefaultShared preferences(本);
编辑等= sp.edit();
et.putLong(时间,S1); //节省时间共享preference中的onPause
et.commit();
} }
3)第3页没有任何变化,我想。
Page3.java 公共无效gobacktopage1(视图V)
{
意图myIntent =新意图(v.getContext(),Page1.class);
startActivity(myIntent);
完();
}
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();
}
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();
}
这篇关于如何暂停,并在安卓启动定时器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!