Hacer que un código específico se ejecute en segundo plano

Me gustaría tener este código ejecutado en segundo plano. No puedo averiguar cómo crear un servicio o Asynstask, como la mayoría señalaría, porque sólo tiene que ser este código, no todo lo demás.

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(); } 

Aquí está el fragmento de código para AsyncTask

 private class AsyncTaskEx extends AsyncTask<Void, Void, Void> { /** The system calls this to perform work in a worker thread and * delivers it the parameters given to AsyncTask.execute() */ @Override protected Void doInBackground(Void... arg0) { StartTimer();//call your method here it will run in background return null; } /** The system calls this to perform work in the UI thread and delivers * the result from doInBackground() */ @Override protected void onPostExecute(Void result) { //Write some code you want to execute on UI after doInBackground() completes return ; } @Override protected void onPreExecute() { //Write some code you want to execute on UI before doInBackground() starts return ; } } 

Escriba esta clase dentro de su Activity y llame donde desee ejecute su método

 new AsyncTaskEx().execute(); 

Usted puede ejecutar su código en el fondo como esto:

 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(); 
  • Android: cacheColorHint = "# 00000000" no funcionan para <ExpandableListView
  • ¿Cómo se restablece el servicio de WhatsApp incluso si debo detener la aplicación?
  • ¿Cómo comprobar todos los servicios en ejecución en android?
  • Android nueve patch no se reconoce en el dispositivo
  • Notificación del temporizador de cuenta regresiva
  • Aplicación para Android: ¿Llamar a AsyncTask dos veces?
  • ¿Cómo saber si mi aplicación Android es visible?
  • Cómo cambiar el fondo clave de cualquier tecla en el teclado suave de Android
  • Cómo animar el fondo de ActionMode de la ActionBar?
  • Android: botón con dos colores de fondo
  • Cómo configurar la imagen de fondo dentro del texto
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.