我有一个 textview ,它是一个 stepuptimer 。我使用了一个处理程序并且每秒更新它并且它完美地工作。但是当我关闭应用程序并打开时,计时器从" 0"开始。我希望时间能够运行后台,当我打开活动时,应显示已用时间,而不是再次从0开始。显示时间格式为 00:00:00 。
我的代码是
public class GoOnlinePage extends Activity {
private TextView Tv_timer;
private Handler onlineTimeHandler = new Handler();
private long startTime = 0L;
private long timeInMilliseconds = 0L;
private long timeSwapBuff = 0L;
private long updatedTime = 0L;
private int mins;
private int secs;
private int hours;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.go_online_page);
Tv_timer = (TextView) findViewById(R.id.txt_timer);
startTime = SystemClock.uptimeMillis();
onlineTimeHandler.post(updateTimerThread);
}
private Runnable updateTimerThread = new Runnable() {
public void run() {
timeInMilliseconds = SystemClock.uptimeMillis() - startTime;
updatedTime = timeSwapBuff + timeInMilliseconds;
secs = (int) (updatedTime / 1000);
hours = secs / (60 * 60);
mins = secs / 60;
secs = secs % 60;
if (mins >= 60) {
mins = 0;
}
Tv_timer.setText(String.format("%02d", hours) + ":" +
String.format("%02d", mins) + ":"
+ String.format("%02d", secs));
onlineTimeHandler.postDelayed(this, 1 * 1000);
}
};
}
我尝试使用会话并在 updateTimerThread 内每秒更新一次。当我打开应用程序时,我得到当前时间并找到两次之间的差异并更新计时器。但没有任何效果。
请帮帮我。在此先感谢。
修改
解决
我使用服务解决了这个问题,并使用 BroadcastReceiver 更新了 textview 。我已附上以下代码供您参考。
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.os.SystemClock;
import android.util.Log;
public class TimerService extends Service {
private static String LOG_TAG = "TimerService";
private IBinder mBinder = new MyBinder();
Handler onlineTimeHandler = new Handler();
long startTime = 0L, timeInMilliseconds = 0L, timeSwapBuff = 0L, updatedTime = 0L;
int mins, secs, hours;
String time = "";
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onCreate() {
super.onCreate();
Log.v(LOG_TAG, "in onCreate");
startTime = SystemClock.uptimeMillis();
onlineTimeHandler.post(updateTimerThread);
}
@Override
public IBinder onBind(Intent intent) {
Log.v(LOG_TAG, "in onBind");
return mBinder;
}
@Override
public void onRebind(Intent intent) {
Log.v(LOG_TAG, "in onRebind");
super.onRebind(intent);
}
@Override
public boolean onUnbind(Intent intent) {
Log.v(LOG_TAG, "in onUnbind");
return true;
}
@Override
public void onDestroy() {
super.onDestroy();
Log.v(LOG_TAG, "in onDestroy");
if(onlineTimeHandler!=null){
onlineTimeHandler.removeCallbacks(updateTimerThread);
}
}
public class MyBinder extends Binder {
TimerService getService() {
return TimerService.this;
}
}
private Runnable updateTimerThread = new Runnable() {
public void run() {
timeInMilliseconds = SystemClock.uptimeMillis() - startTime;
updatedTime = timeSwapBuff + timeInMilliseconds;
secs = (int) (updatedTime / 1000);
hours = secs / (60 * 60);
mins = secs / 60;
secs = secs % 60;
if (mins >= 60) {
mins = 0;
}
time = String.format("%02d", hours) + ":" + String.format("%02d", mins) + ":"
+ String.format("%02d", secs);
Intent intent = new Intent();
intent.setAction("com.app.Timer.UpdateTime");
intent.putExtra("time", time);
sendBroadcast(intent);
onlineTimeHandler.postDelayed(this, 1 * 1000);
}
};
}
答案 0 :(得分:1)
当计时器在某个持久位置(如SharedPreferences等)启动时存储startTime
值,并在计时器结束时删除。
在您的Activity#onCreate
方法中,检查该持久值是否存在,请使用该值而非当前系统时间初始化您的startTime
值。
与相关代码相关的一些提示:
new Date().getTime()
代替SystemClock.uptimeMillis()
。它使您的代码在重新启动时更加强大。Activity#onPause
被叫时,请停止计时器自动收报机,并在Activity#onResume
被叫时启动。因为您不需要更新您的用户界面,因为它不可见。答案 1 :(得分:1)
使用一些静态变量非常简单。 即使活动在后台或被破坏,此示例也会继续计数,不需要SharedPreferences或服务:
public class MainActivity extends AppCompatActivity {
private static TextView txtCounter;
private static Handler handler;
private static Runnable runnable ;
private static int count;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtCounter = (TextView) findViewById(R.id.txtCounter);
if (handler == null) {
handler = new Handler();
runnable = new Runnable() {
@Override
public void run() {
txtCounter.setText("Count = " + count);
count++;
handler.postDelayed(runnable, 1000);
}
};
handler.postDelayed(runnable, 1000);
}
}
}
<强>更新强>
暂停:handler.removeCallbacks(runnable);
要恢复:handler.postDelayed(runnable, 1000);
要重置计数器:count = 0;
答案 2 :(得分:0)
当您的活动被销毁时,Tv_timer对象也将被销毁,线程正在更新。当活动再次启动时,会创建Tv_timer的新对象。
此外,时间可以存储在某处,并在以后启动应用程序时使用
答案 3 :(得分:0)
实现这一目标有两种方法: