我想让这段代码在后台运行。正如大多数人所指出的那样,我无法弄清楚如何创建服务或 Asynstask,因为它只需要是这段代码,而不是其他所有内容。
void StartTimer()
{
int minsTicks=CountM*60*1000;
int hoursTicks=CountT*60*60*1000;
int totalTicks=hoursTicks+minsTicks;
mTextField = (TextView) findViewById(R.id.TimerTextView);
CountDownTimer aCounter = new CountDownTimer(totalTicks, 1000)
{
public void onTick(long millisUntilFinished)
{
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish()
{
try
{
mTextField.setText("Kaffe Maskinen er igang");
mmOutputStream.write('2');
Thread.sleep(900000);
mmOutputStream.write('0');
mTextField.setText("Kaffe Maskinen er slukket");
}
catch (IOException e) {} catch (InterruptedException e) {}
}
};
aCounter.start();
}