正如标题所示,我在我的应用程序中使用倒数计时器,我希望用户能够退出应用程序,同时在后台运行倒数计时器并根据更新文本视图。我假设需要一项服务,但我不知道如何在我的应用程序中为倒计时器实现服务。下面提供的代码块将说明我对注释的问题。
public class CountDownService extends Service {
TextView timeTextView;
int data;
public String hms;
public CountDownAct countDownAct;
public CountDownTime countDownTimer;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
data = intent.getIntExtra("the", 0);
countDownTimer = new CountDownTime(data,1000 );
countDownTimer.start();
return START_STICKY;
}
@Override
public boolean stopService(Intent name) {
Log.i("CountDownService", "Stop Service");
return super.stopService(name);
}
@Override
public void onDestroy() {
countDownTimer.cancel();
}
public class CountDownTime extends CountDownTimer {
/**
* @param millisInFuture The number of millis in the future from the call
* to {@link #start()} until the countdown is done and {@link #onFinish()}
* is called.
* @param countDownInterval The interval along the way to receive
* {@link #onTick(long)} callbacks.
*/
public CountDownTime(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onTick(long millisUntilFinished) {
long millis = millisUntilFinished;
// this is the format which reads the time data from the user and makes it readable
hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis),
TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
//the textview is updated with each tick that goes by and contains the current time that is left
// I want this text to be updated even if the user exits the app. The textview has to be updated with the current time left until finishing
// countDownAct.timeTextView.setText(hms);
CountDownAct.timeTextView.setText(hms);
Log.i("CountDownService", hms);
}
@Override
public void onFinish() {
Intent goBack = new Intent(getApplicationContext(), alarmtimefinished.class);
goBack.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
goBack.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(goBack);
}
}
}
07-24 18:27:41.629 19054-19054/com.personalproject.peter.timerapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at com.personalproject.peter.timerapp.CountDownService$CountDownTime.onTick(CountDownService.java:79)
at android.os.CountDownTimer$1.handleMessage(CountDownTimer.java:124)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4867)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
我最近一直在使用服务而且非常简单,只需创建一个扩展服务的类,检查一下:http://developer.android.com/guide/components/services.html
将您的代码放在onStartCommand方法
中为什么不在服务类
中创建计时器的对象如果您希望您的服务重复执行某项操作,请执行以下操作:
Handler mHandler;
private void doSomething() {
scheduleNext(10*1000); // time in m seconds
//your code here
}
private void scheduleNext(int time) {
mHandler.postDelayed(new Runnable() {
public void run() {
doSomething();
}
},time);
我希望这会对你有所帮助