Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - how to receive broadcast intents ACTION_SCREEN_ON/OFF?

    <application>          <receiver android:name=".MyBroadcastReceiver" android:enabled="true">                 <intent-filter>                       <action android:name="android.intent.action.ACTION_SCREEN_ON"></action>                       <action android:name="android.intent.action.ACTION_SCREEN_OFF"></action>                 </intent-filter>          </receiver> ...     </application> 

MyBroadcastReceiver is set just to spit foo to the logs. Does nothing. Any suggestions please? Do I need to assign any permissions to catch the intent?

like image 689
ohnoes Avatar asked Oct 19 '09 11:10

ohnoes


People also ask

How do I turn off broadcast receiver?

To stop receiving broadcasts, call unregisterReceiver(android. content. BroadcastReceiver) . Be sure to unregister the receiver when you no longer need it or the context is no longer valid.

What is used to listen for broadcast intents?

Broadcast Receivers are used to listen for Broadcast Intents.

What are receiving and broadcasting intents in Android?

Broadcast intents are a mechanism by which an intent can be issued for consumption by multiple components on an Android system. Broadcasts are detected by registering a Broadcast Receiver which, in turn, is configured to listen for intents that match particular action strings.

What is broadcasting receiver in android?

A broadcast receiver (receiver) is an Android component which allows you to register for system or application events. All registered receivers for an event are notified by the Android runtime once this event happens.


2 Answers

I believe that those actions can only be received by receivers registered in Java code (via registerReceiver()) rather than through receivers registered in the manifest.

like image 71
CommonsWare Avatar answered Oct 14 '22 12:10

CommonsWare


Alternatively you can use the power manager to detect screen locking.

@Override protected void onPause() {     super.onPause();      // If the screen is off then the device has been locked     PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);     boolean isScreenOn;     if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {         isScreenOn = powerManager.isInteractive();     } else {         isScreenOn = powerManager.isScreenOn();     }      if (!isScreenOn) {          // The screen has been locked          // do stuff...     } } 
like image 44
Robert Avatar answered Oct 14 '22 13:10

Robert