Aquí hay una forma semi-diferente de mantener el servicio en funcionamiento para siempre. Hay maneras de matar en el código si lo desea
servicio en segundo plano:
package com.ex.ample;
import android.app.Service;
import android.content.*;
import android.os.*;
import android.widget.Toast;
public class BackgroundService extends Service {
public Context context = this;
public Handler handler = null;
public static Runnable runnable = null;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
Toast.makeText(this, "Service created!", Toast.LENGTH_LONG).show();
handler = new Handler();
runnable = new Runnable() {
public void run() {
Toast.makeText(context, "Service is still running", Toast.LENGTH_LONG).show();
handler.postDelayed(runnable, 10000);
}
};
handler.postDelayed(runnable, 15000);
}
@Override
public void onDestroy() {
/* IF YOU WANT THIS SERVICE KILLED WITH THE APP THEN UNCOMMENT THE FOLLOWING LINE */
//handler.removeCallbacks(runnable);
Toast.makeText(this, "Service stopped", Toast.LENGTH_LONG).show();
}
@Override
public void onStart(Intent intent, int startid) {
Toast.makeText(this, "Service started by user.", Toast.LENGTH_LONG).show();
}
}
Así es como se inicia desde su actividad principal o dondequiera que usted desee:
startService(new Intent(this, BackgroundService.class));
onDestroy()
se llamará cuando la aplicación se cierre o muera, pero el ejecutable simplemente lo inicia de nuevo.
Espero que esto ayude a alguien.
La razón por la que algunas personas hacen esto es debido a las aplicaciones corporativas, donde en algunos casos los usuarios/empleados no deben ser capaces de dejar ciertas cosas :)
http://i.imgur.com/1vCnYJW.png
probablemente más fácil de hacer algo como 'Sistema .currentTimeMillis() + TIMEOUT_MS' que rebuscar con el 'Calendario', para obtener su retraso inicial. –
Ustedes saben que tienen valores estáticos en el AlarmManager por 1 día, 12 horas, 1 hora, media hora y 15 minutos? 'public static final de INTERVAL_DAY' largo ' pública INTERVAL_FIFTEEN_MINUTES' largo static final 'pública INTERVAL_HALF_DAY' largo static final ' pública INTERVAL_HALF_HOUR' static final largo 'pública INTERVAL_HOUR' largo static final – DaMachk