您可以在后台执行代码,如下所示:
private class GetLoginResponse extends AsyncTask<Void, Void, Boolean> { @Override protected void onPreExecute() { // whatever code u want to execute before background code // starts executing } @Override protected Boolean doInBackground(Void... params) { 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(); } @Override protected void onPostExecute(Boolean data) { // Here you can execute what you want to execute // after the background task completes } }// end AsyncTask new GetLoginResponse.execute();