2011-09-09 35 views
7

Estoy tratando de desactivar el WiFi cuando la pantalla está apagada (bloqueada) y volver a encenderla cuando la pantalla está encendida (desbloqueada).ACTION_SCREEN_ON y ACTION_SCREEN_OFF no funcionan?

Hice un BroadcastReceiver; puesto de manifiesto en este código:

<receiver android:name="MyIntentReceiver"> 
      <intent-filter> 
       <action android:name="android.intent.action.BOOT_COMPLETED" /> 
       <action android:name="android.intent.action.SCREEN_OFF" /> 
       <action android:name="android.intent.action.SCREEN_ON" /> 
       <action android:name="android.intent.action.USER_PRESENT" /> 
       <category android:name="android.intent.category.HOME" /> 
       <category android:name="android.intent.category.LAUNCHER" />  
      </intent-filter> 
     </receiver> 

y esta es la clase MyIntentReceiver:

package org.androidpeople.boot; 

import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 

public class MyIntentReceiver extends BroadcastReceiver { 
    // Called when boot completes 

    public static boolean startup; 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     // Set what activity should launch after boot completes 

     System.out.println("Intent Action: " + intent.getAction()); 

     if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) { 

      System.out.println("locked : ACTION_SCREEN_OFF"); 

     } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) { 

      System.out.println("not locked : ACTION_SCREEN_ON "); 

     } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) { 

      System.out.println("User Unlocking it "); 

     } 
     else if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) { 
      // this to indicate that program is running 
      // automaticlly not manually by user 
      startup = true; 
      System.out.println("Automatic BOOT at StartUp"); 

      Intent startupBootIntent = new Intent(context, LaunchActivity.class); 
      startupBootIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
      context.startActivity(startupBootIntent); 
     } 

    } 
} 

Y el resultado es - tanto ACTION_SCREEN_ON y ACTION_SCREEN_OFF nunca se disparó! USER_PRESENT y BOOT_COMPLETED funcionó bien, pero el otro no. Estoy usando un emulador, no un dispositivo real. ¿Esto puede causar el problema?

¿Algún ayuda? Necesito encender y apagar la pantalla para habilitar/deshabilitar WiFi para ahorrar batería.

Gracias de antemano

+0

http://thinkandroid.wordpress.com/2010/01/24/handling-screen-off-and-screen-on-intents/ –

Respuesta

8

No se puede coger esos intentos a través de XML (no recuerdo por qué). Sin embargo, puede usar un Service que registra un miembro BroadcastReceiver en su onStartCommand() y lo anula en su onDestroy(). Esto requeriría que el servicio se ejecute en segundo plano, constantemente o durante el tiempo que lo necesite, así que asegúrese de explorar rutas alternativas.

Se podría definir el BroadcastReceiver en su clase Service así:

private final class ScreenReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(final Context context, final Intent intent) { 
     if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) { 
      //stuff 
     } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) { 
      //other stuff 
     } 
    } 
} 

Para un ejemplo un poco complicado, pero que muestra cómo interactúan BroadcastReceiver y ServiceCheckForScreenBugAccelerometerService ver desde mi aplicación, ElectricSleep.

12

Para capturar las acciones SCREEN_OFF y SCREEN_ON (y tal vez otras), debe configurar el BroadcastReceiver por código, no a través del manifiesto.

IntentFilter intentFilter = new IntentFilter(Intent.ACTION_SCREEN_ON); 
intentFilter.addAction(Intent.ACTION_SCREEN_OFF); 
BroadcastReceiver mReceiver = new ScreenStateBroadcastReceiver(); 
registerReceiver(mReceiver, intentFilter); 

Ha sido probado y funciona bien.

+0

+1, para IntentFilter utiliza el encendido y para la acción el apagado? –

+0

Ambas son acciones configuradas en IntentFilter. – PoOk

+0

He agregado de esta manera, pero cuando borro la aplicación reciente, el método de recepción de recepción de Receptor no responde. Si la aplicación que se está ejecutando y luego enReceive funciona bien. –

Cuestiones relacionadas