android: broadcast receiver for screen on and screen off
The two actions for screen on and off are:
android.intent.action.SCREEN_OFF
android.intent.action.SCREEN_ON
But if you register a receiver for these broadcasts in a manifest, then the receiver will not receive these broadcasts.
For this problem, you have to create a long running service, which is registering a local broadcast receiver for these intents. If you do this way, then your app will look for screen off only when your service is running which won't irritate user.
PS: start the service in foreground to make it running longer.
A simple code snippet will be something like this:
IntentFilter screenStateFilter = new IntentFilter();
screenStateFilter.addAction(Intent.ACTION_SCREEN_ON);
screenStateFilter.addAction(Intent.ACTION_SCREEN_OFF);
registerReceiver(mScreenStateReceiver, screenStateFilter);
Don't forget to unregister the receiver in the Service's onDestroy:
unregisterReceiver(mScreenStateReceiver);
Just in case for people who are asking why the receiver does not work with the declare broadcasts in manifest for ACTION_SCREEN_ON and ACTION_SCREEN_OFF:
https://developer.android.com/reference/android/content/Intent.html#ACTION_SCREEN_ON https://developer.android.com/reference/android/content/Intent.html#ACTION_SCREEN_OFF
You cannot receive this through components declared in manifests, only
by explicitly registering for it with Context.registerReceiver().This is a protected intent that can only be sent by the system.
Why isn't my Screen On/Off BroadcastReceiver working?
You can do this by trying dynamic calling of broadcast like below
public class YourActivity extends Activity {
//Create broadcast object
BroadcastReceiver mBroadcast = new BroadcastReceiver() {
//When Event is published, onReceive method is called
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.i("[BroadcastReceiver]", "MyReceiver");
if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
Log.d("[BroadcastReceiver]", "Screen ON");
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
Log.d("[BroadcastReceiver]", "Screen OFF");
}
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
registerReceiver(mBroadcast, new IntentFilter(Intent.ACTION_SCREEN_ON));
registerReceiver(mBroadcast, new IntentFilter(Intent.ACTION_SCREEN_OFF));
}
}
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
.
BroadcastReceiver for Screen On/Off not working
Hey try using dynamic calling of broadcast,I tried this it will surly work...
public class MainActivity extends Activity {
//Create broadcast object
BroadcastReceiver mybroadcast = new BroadcastReceiver() {
//When Event is published, onReceive method is called
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.i("[BroadcastReceiver]", "MyReceiver");
if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
Log.i("[BroadcastReceiver]", "Screen ON");
}
else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
Log.i("[BroadcastReceiver]", "Screen OFF");
}
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
registerReceiver(mybroadcast, new IntentFilter(Intent.ACTION_SCREEN_ON));
registerReceiver(mybroadcast, new IntentFilter(Intent.ACTION_SCREEN_OFF));
}
}
Related Topics
"No Cached Version... Available for Offline Mode."
Setbackground VS Setbackgrounddrawable (Android)
Android Recyclerview Gridlayoutmanager Column Spacing
How to Use Custom Font in a Project Written in Android Studio
Simplecursortreeadapter and Cursorloader for Expandablelistview
How to Programmatically Move, Copy and Delete Files and Directories on Sd
Android: Upgrading Db Version and Adding New Table
Eclipse Giving Error, Missing R.Java File After Recent Update
How to Configure Gradle to Work "Offline" (Using Cached Dependencies)
How to Run a Method Every X Seconds
Android: Want to Set Custom Fonts for Whole Application Not Runtime
Receiving Package Install and Uninstall Events
How to Attach Javadoc or Sources to Jars in Libs Folder
Android: Unable to Add Window. Permission Denied for This Window Type
Android: Remove Left Margin from Actionbar's Custom Layout