即使关闭应用程序,如何保持CountDownTimer运行? [英] How to keep a CountDownTimer running even if the app is closed?
问题描述
我度过了整个暑假,学习如何编码和构建自己的第一个应用程序(在android studio中).因此,我不是专家编码员.这周我遇到了一个问题.基本上,我正在开发一个测验应用程序,当用户输入错误答案时,他必须等待5分钟.事实是,当倒数计时器启动并且用户关闭应用程序时(我指的是关闭应用程序,将其销毁,而不仅仅是按下主页按钮),倒数计时器将停止.我想知道即使关闭应用程序也如何保持计时器运行.您的帮助将不胜感激,如果你们给我一个示例代码,那将是非常棒的. 预先感谢!
I spent my summer learning how to code and building my first app (in android studio). Therefore I am not an expert coder. This week I encountered a problem. Basically, I am developing a quiz app and when the user gets the answer wrong he have to wait 5 min. The thing is that when the countdowntimer starts and the user closes the app (by closing the app I mean destroy it and not just push the home button) the countdowntimer stops. I am wondering how I can manage to keep the timer running even if the app is closed. Your help will be greatly appreciated and it would be fantastic if you guys give me a sample code. Thanks in advance!
推荐答案
以如下方式在服务中运行:在您的活动中使用创建广播接收器,并让该服务发送广播.
Run it in a service as such: use create a broadcast receiver in your activity and have the service send broadcasts.
package com.example.cdt;
import android.app.Service;
import android.content.Intent;
import android.os.CountDownTimer;
import android.os.IBinder;
import android.util.Log;
public class BroadcastService extends Service {
private final static String TAG = "BroadcastService";
public static final String COUNTDOWN_BR = "your_package_name.countdown_br";
Intent bi = new Intent(COUNTDOWN_BR);
CountDownTimer cdt = null;
@Override
public void onCreate() {
super.onCreate();
Log.i(TAG, "Starting timer...");
cdt = new CountDownTimer(30000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
Log.i(TAG, "Countdown seconds remaining: " + millisUntilFinished / 1000);
bi.putExtra("countdown", millisUntilFinished);
sendBroadcast(bi);
}
@Override
public void onFinish() {
Log.i(TAG, "Timer finished");
}
};
cdt.start();
}
@Override
public void onDestroy() {
cdt.cancel();
Log.i(TAG, "Timer cancelled");
super.onDestroy();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
}
主要活动中:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startService(new Intent(this, BroadcastService.class));
Log.i(TAG, "Started service");
}
private BroadcastReceiver br = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
updateGUI(intent); // or whatever method used to update your GUI fields
}
};
@Override
public void onResume() {
super.onResume();
registerReceiver(br, new IntentFilter(BroadcastService.COUNTDOWN_BR));
Log.i(TAG, "Registered broacast receiver");
}
@Override
public void onPause() {
super.onPause();
unregisterReceiver(br);
Log.i(TAG, "Unregistered broacast receiver");
}
@Override
public void onStop() {
try {
unregisterReceiver(br);
} catch (Exception e) {
// Receiver was probably already stopped in onPause()
}
super.onStop();
}
@Override
public void onDestroy() {
stopService(new Intent(this, BroadcastService.class));
Log.i(TAG, "Stopped service");
super.onDestroy();
}
private void updateGUI(Intent intent) {
if (intent.getExtras() != null) {
long millisUntilFinished = intent.getLongExtra("countdown", 0);
Log.i(TAG, "Countdown seconds remaining: " + millisUntilFinished / 1000);
}
}
请注意,我从>如何在是Android中的服务?,我为自己的android countDownTimer对其进行了修改.
Note I got this code from How to run CountDownTimer in a Service in Android?, which I modified for my own android countDownTimer.
这篇关于即使关闭应用程序,如何保持CountDownTimer运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!