Listening to SCREEN_OFF action in android without registering

530 Views Asked by At

People. I want to listen to on SCREEN_OFF of android for that I have declared a broadcast-receiver as

public class OnListenWOABC extends BroadcastReceiver {


@Override
public void onReceive(Context context_ext, Intent intent) {



     Log.d("lock","Screen went OFF");





    } 



}

And in my menifest.xml I have done as

<receiver android:exported="false" android:name="com.myapp.demo.OnListenWOABC">
<intent-filter>  
<action  android:name="android.intent.action.SCREEN_OFF" />  
</intent-filter>  
</receiver> 

But my Receiver is not listening at all. The problem is , I don't want to register the reciver in any activity, and want it to start without launching the app. on BOOT is working without even registering in any class. Please help me out, and let me know if it is possible. Thank you

2

There are 2 best solutions below

10
On

The SCREEN_OFF broadcast can't be registered in manifest, it must be registered in java code.The following actions can't be registered in manifest:

android.intent.action.SCREEN_ON
android.intent.action.SCREEN_OFF
android.intent.action.BATTERY_CHANGED
android.intent.action.CONFIGURATION_CHANGED
android.intent.action.TIME_TICK
0
On

How about TelephonyManager.EXTRA_STATE_RINGING which detects incoming call.