Detener el servicio cuando se inicia un proceso externo

Is it possible to stop a service when an external application has been started?

For an activity you can use getRunningAppProcesses() and check in the list if the app you want is running. But what about a service?

preguntado el 09 de marzo de 12 a las 15:03

tratan getRunningServices() método de ActivityManager -

but I need to run it in a service, not in an activity -

ACTIVITY_SERVICE also available in your Service -

1 Respuestas

Utilizar esta:

public class TestService extends Service {

    @Override
    public void onCreate()
    {
        ActivityManager am = (ActivityManager)this.getSystemService(ACTIVITY_SERVICE);
        List rs = am.getRunningServices(50);

        for (int i=0; i<rs.size(); i++) {
        ActivityManager.RunningServiceInfo rsi = (RunningServiceInfo) rs.get(i);
        Log.i("Service", "Process " + rsi.process + " with component " + rsi.service.getClassName());
        }
        super.onDestroy();
        Toast.makeText(getApplicationContext(),"1. onCreate()", Toast.LENGTH_SHORT).show();
    }
}

respondido 09 mar '12, 17:03

So my service will need to know check every X seconds if the process is available and than stop itself. This would drain battery, am I right? - Ángel Tricarico

Thanks for the answer. But is there another way which doesn't affect the battery? - Ángel Tricarico

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.