2011-09-09 2 views
7

Sto provando a disattivare il Wi-Fi quando lo schermo è spento (bloccato) e lo riaccendo quando lo schermo è acceso (sbloccato).ACTION_SCREEN_ON e ACTION_SCREEN_OFF non funzionano?

Ho fatto un BroadcastReceiver; messo in palese di questo codice:

<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> 

e questa è la classe 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); 
     } 

    } 
} 

E il risultato è - sia ACTION_SCREEN_ON e ACTION_SCREEN_OFF mai sparato! USER_PRESENT e BOOT_COMPLETED funzionavano bene ma l'altro no. Sto usando un emulatore, non un vero dispositivo - può questo causare il problema?

Qualsiasi aiuto? Ho bisogno di acchiappare lo schermo per abilitare/disabilitare WiFi per risparmiare batteria.

Grazie in anticipo

+0

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

risposta

8

Non si può prendere questi intenti tramite XML (non ricordo il motivo). Tuttavia, è possibile utilizzare uno Service che registri un membro BroadcastReceiver nella sua onStartCommand() e lo annulli nella sua onDestroy(). Ciò richiederebbe che il servizio fosse eseguito in background, costantemente o fino a quando ne hai bisogno, quindi assicurati di esplorare percorsi alternativi.

Si potrebbe definire il BroadcastReceiver nella classe Service in questo modo:

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 
     } 
    } 
} 

Per un esempio un po 'complicato, ma che mostra come l'interagiscono BroadcastReceiver e Service vedere CheckForScreenBugAccelerometerService dalla mia app, ElectricSleep.

12

Per acquisire le azioni SCREEN_OFF e SCREEN_ON (e forse altre) è necessario configurare BroadcastReceiver in base al codice, non tramite il manifest.

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

È testato e funziona correttamente.

+0

+1, per IntentFilter si utilizza ON e per l'azione OFF? –

+0

Entrambe le azioni sono impostate su IntentFilter. – PoOk

+0

Ho aggiunto in questo modo ma quando cancello l'app recente, il metodo di BroadcastReceiver suReceive non risponde. Se l'app in esecuzione allora onReceive funziona correttamente. –