2012-07-05 12 views
17

Próbuję uruchomić usługę, która działa w tle, który nasłuchuje na ACTION_SCREEN_OFF, a kiedy znajdzie ACTION_SCREEN_OFF, rozpoczyna moją działalność.Słuchanie dla ACTION_SCREEN_OFF

Czytam gdzieś, gdzie trzeba utworzyć BroadcastReceiver, ponieważ umieszczenie go w manifeście XML nie działa. Jednak nie mam pojęcia, od czego zacząć po wielu poszukiwaniach.

Odpowiedz

36

Nie można zadeklarować wartości ACTION_SCREEN_ON i w urządzeniu AndroidManifest.xml . Możesz je złapać tylko podczas aktywności.

Oto przykład.

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

} 

aktywny:

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(); 
    } 

} 

Można prawdopodobnie rozwiązać swoje pytanie słuchając tych zdarzeniach z Service.

+1

Problem polega na tym, że funkcje 'onResume()' i 'onPause()' są wywoływane przed 'BroadcastReceiver.onReceive()'. –

+0

To nie jest problem. Dlatego ustawiamy status ekranu na zmienną ('wasScreenOn'). – DragonWork

+0

Czy tu czegoś brakuje? Po opuszczeniu aktywności za pomocą przycisku Wstecz lub przycisku strony głównej ScreenReceiver.wasScreenOn ma wartość PRAWDA, więc funkcja onPause() będzie myśleć, że ekran wyłącza się, gdy faktycznie działanie jest po prostu wstrzymane. –

Powiązane problemy