暂停和重启定时器使用作出处理 [英] Pause and Resume timer made using handlers
本文介绍了暂停和重启定时器使用作出处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面是我的$ C $下的活动。在此,我打印的文本视图经过的时间。我想提出的onPause和onResume方法在此。它应该工作,使得时间暂停,而应用程序是在背景。当再一次被送往前线,应定时从那里暂停开始。我尝试过用这个code,但当时没有被暂停。它继续在后台运行。任何人可以帮助找到解决此。
包com.example.test;
进口android.support.v7.app.ActionBarActivity;
进口android.app.Activity;
进口android.content.Intent;
进口android.os.Bundle;
进口android.os.Handler;
进口android.view.Menu;
进口android.view.MenuItem;
进口android.widget.TextView;
公共类MainActivity延伸活动{
TextView的TT1;
私人处理程序customHandler =新的处理程序();
长timeInMilliseconds = 0L,timeToGo = 0L,的startTime = 0L;
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
TT1 =(TextView中)findViewById(R.id.textView1);
的startTime = System.currentTimeMillis的();
customHandler.postDelayed(updateTimerThread,0);
}
市民可运行updateTimerThread =新的Runnable(){
@覆盖
公共无效的run(){
// TODO自动生成方法存根
长timeNow = System.currentTimeMillis的();
timeToGo = 30 - (timeNow - startTime时)/ 1000;
TT1 =(TextView中)findViewById(R.id.textView1);
tt1.setText(timeToGo +);
如果(timeToGo< 0L){
意向意图=新的意图(MainActivity.this,Game.class);
完();
startActivity(意向);
}
其他
customHandler.postDelayed(此,0);
}
};
@覆盖
公共无效的onPause(){
super.onPause();
customHandler.removeCallbacksAndMessages(空);
}
@覆盖
公共无效onResume(){
super.onResume(); //总是先调用父类的方法
customHandler.postDelayed(updateTimerThread,0);
}
}
解决方案
@覆盖
保护无效的onPause(){
customHandler.removeCallbacks(updateTimerThread);
super.onPause(); }
Here is my code for an activity. In this I am printing the time elapsed on a text view. I want to put onPause and onResume method in this. It should work such that the time is paused while app is in background . And when again brought to forefront, should timer start from where it paused. I tried it using this code but the time is not paused. It continues to run in background. Can anybody help to find workaround for this.
package com.example.test;
import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView tt1;
private Handler customHandler = new Handler();
long timeInMilliseconds = 0L,timeToGo=0L,startTime=0L;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tt1=(TextView) findViewById(R.id.textView1);
startTime=System.currentTimeMillis();
customHandler.postDelayed(updateTimerThread, 0);
}
public Runnable updateTimerThread=new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
long timeNow = System.currentTimeMillis();
timeToGo = 30 - (timeNow - startTime) / 1000;
tt1=(TextView) findViewById(R.id.textView1);
tt1.setText(timeToGo+"");
if(timeToGo<0L){
Intent intent=new Intent(MainActivity.this,Game.class);
finish();
startActivity(intent);
}
else
customHandler.postDelayed(this, 0);
}
};
@Override
public void onPause() {
super.onPause();
customHandler.removeCallbacksAndMessages(null);
}
@Override
public void onResume() {
super.onResume(); // Always call the superclass method first
customHandler.postDelayed(updateTimerThread, 0);
}
}
解决方案
@Override
protected void onPause() {
customHandler.removeCallbacks(updateTimerThread);
super.onPause(); }
这篇关于暂停和重启定时器使用作出处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文