2012-07-05 13 views
17

Estoy tratando de iniciar un servicio que se ejecuta en segundo plano que está escuchando ACTION_SCREEN_OFF y cuando encuentra ACTION_SCREEN_OFF, comienza mi actividad.Escuchando por ACTION_SCREEN_OFF

Leo en algún lugar que necesita para crear un BroadcastReceiver porque ponerlo en el manifiesto XML no funciona. Sin embargo, no tengo idea de dónde empezar después de mucha búsqueda.

Respuesta

36

No puede declarar ACTION_SCREEN_ON y ACTION_SCREEN_OFF en el AndroidManifest.xml. Solo puede atraparlos mientras se ejecuta su actividad.

Aquí hay un ejemplo.

El BroadcastReceiver:

public class ScreenReceiver extends BroadcastReceiver { 

    public static boolean wasScreenOn = true; 

    @Override 
    public void onReceive(final Context context, final Intent intent) { 
     if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) { 
      // do whatever you need to do here 
      wasScreenOn = false; 
     } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) { 
      // and do whatever you need to do here 
      wasScreenOn = true; 
     } 
    } 

} 

El Actividad:

public class ExampleActivity extends Activity { 

    private BroadcastReceiver mReceiver = null; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     // initialize receiver 
     final IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON); 
     filter.addAction(Intent.ACTION_SCREEN_OFF); 
     mReceiver = new ScreenReceiver(); 
     registerReceiver(mReceiver, filter); 
     // your code 
    } 

    @Override 
    protected void onPause() { 
     // when the screen is about to turn off 
     if (ScreenReceiver.wasScreenOn) { 
      // this is the case when onPause() is called by the system due to a screen state change 
      Log.e("MYAPP", "SCREEN TURNED OFF"); 
     } else { 
      // this is when onPause() is called when the screen state has not changed 
     } 
     super.onPause(); 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     // only when screen turns on 
     if (!ScreenReceiver.wasScreenOn) { 
      // this is when onResume() is called due to a screen state change 
      Log.e("MYAPP", "SCREEN TURNED ON"); 
     } else { 
      // this is when onResume() is called when the screen state has not changed 
     } 
    } 

    @Override 
    protected void onDestroy() { 
     if (mReceiver != null) { 
      unregisterReceiver(mReceiver); 
      mReceiver = null; 
     } 
     super.onDestroy(); 
    } 

} 

Probablemente se podría resolver su pregunta al escuchar a estos eventos de un Service.

+1

El problema aquí es que las actividades 'onResume()' y 'onPause()' se invocan antes que 'BroadcastReceiver.onReceive()'. –

+0

Eso no es un problema. Por lo tanto, establecemos el estado de la pantalla en una variable ('wasScreenOn'). – DragonWork

+0

¿Me estoy perdiendo algo aquí? Cuando salgas de tu actividad con el botón Atrás o el botón de inicio, ScreenReceiver.wasScreenOn es TRUE, por lo que onPause() pensará que la pantalla se está apagando cuando, de hecho, tu Actividad se está pausando. –

Cuestiones relacionadas