Listening for ACTION_SCREEN_OFF
You cannot declare ACTION_SCREEN_ON
and ACTION_SCREEN_OFF
in the AndroidManifest.xml.
You are only allowed to catch them while your activity is running.
Here's an example.
The 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;
}
}
}
The Activity:
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();
}
}
You could probably solve your question by listening to these events from a Service
.
Listen for screen power on / power off even after onDestroy()
If you register your ScreenReceiver
in your AndroidManifest
, you'll get the broadcast even if your app has been destroyed. The only time you won't get the broadcast is if the user force stops the app through settings
something like
<receiver android:name=".ScreenReceiver">
<intent-filter>
<action android:name="action_looking_for"/>
</intent-filter>
</receiver>
BroadcastReceiver isn't receiving ACTION_SCREEN_OFF intent
ACTION_SCREEN_OFF
is not sent to receivers registered in the manifest. It is only sent to receivers registered via registerReceiver()
from a running component.
Android Intent.ACTION_SCREEN_OFF and Intent.ACTION_USER_PRESENT how to register it
You can not register these receiver from android manifest file. Its not at all supporting. The only way to do it is a long running services and register these receiver inside the service. So if you really want to use
ACTION_SCREEN_ON
AND
ACTION_SCREEN_OFF
Then you have to use service
Open activity when screen goes off
There is no more efficient way. When your app launches the Activity from the SCREEN_OFF broadcast, Android creates the Activity then immediately after calls that Activity's onPause() method. Different phones handle it differently however, so things like shadowing, and ghosting are expected just because when you turn the screen on again, the Activity's onResume() is called, so a refresh sometimes happens, depening on device.
Related Topics
Why Getspeed() Always Return 0 on Android
Touch and Drag Image in Android
How to Use the Gradle Build System for Android with Eclipse
How to Draw Border on Just One Side of a Linear Layout
How to Set the Focus (And Display the Keyboard) on My Edittext Programmatically
How to Get the Mobile Number of Current Sim Card in Real Device
How to Check If Facebook Is Installed Android
How to Set the Airplane_Mode_On to "True" or On
How to Set Alarm in Android Programmatically
Java.Lang.Noclassdeffounderror: Com.Google.Android.Gms.Internal.Zzmp
How to Ask Runtime Permissions for Camera in Android , Runtime Storage Permissions