21.04.2014 Views

Curso-Android-Desarrollo-de-Aplicaciones-Móviles

Curso-Android-Desarrollo-de-Aplicaciones-Móviles

Curso-Android-Desarrollo-de-Aplicaciones-Móviles

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CURSO ANDROID<br />

87<br />

public void onCreate() {<br />

Toast.makeText(this, “Servicio Creado”, Toast.LENGTH_LONG).show();<br />

player = MediaPlayer.create(this, R.raw.water_droplet);<br />

player.setLooping(true);<br />

}<br />

@Overri<strong>de</strong><br />

public void onDestroy() {<br />

Toast.makeText(this, “Servicio Detenido”, Toast.LENGTH_LONG).show();<br />

player.stop();<br />

}<br />

@Overri<strong>de</strong><br />

public void onStart(Intent intent, int startid) {<br />

Toast.makeText(this, “Servicio Iniciado”, Toast.LENGTH_LONG).show();<br />

player.start();<br />

}<br />

}<br />

Este código <strong>de</strong>l servicio se ejecutará como una tarea en Background es <strong>de</strong>cir podríamos cerrar la aplicación<br />

y la ejecución continuaría. Por lo mismo, adicional al servicio, es necesario construir código para<br />

iniciarlo y <strong>de</strong>tenerlo.<br />

Esto lo haremos en nuestra actividad principal asociando acciones a los botones previamente mencionados<br />

en el diseño. Dado que tenemos 2 botones que tendrán el mismo listener en lugar <strong>de</strong> instanciar<br />

un OnClickListener como una clase anónima nuestra Actividad implementará a OnClickListener.<br />

public class Main extends Activity implements OnClickListener<br />

Dentro <strong>de</strong>l método onCreate <strong>de</strong> la actividad asociamos el listener a los botones.<br />

Button btnInicio = (Button) findViewById(R.id.btnInicio);<br />

Button btnFin = (Button) findViewById(R.id.btnFin);<br />

btnInicio.setOnClickListener(this);<br />

btnFin.setOnClickListener(this);<br />

Por último es necesario sobrecargar el método onClick y <strong>de</strong>ntro <strong>de</strong> él distinguimos que botón fue presionado<br />

y realizamos la acción asociada en cada caso.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!