domingo, 28 de octubre de 2012

How to check if a service is running?


Problem:

Need to check if some service is running.


Solution:

Create a method as the following


private boolean isMyServiceRunning() {
    ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
        if ("com.example.MyService".equals(service.service.getClassName())) {
            return true;
        }
    }
    return false;

No hay comentarios:

Publicar un comentario