How can I tell if the screen is on in android?
You can accomplish this by setting up broadcast receivers for ACTION_SCREEN_ON
and ACTION_SCREEN_OFF
.
How to tell if screen is on with ADB
In doing some testing I've found that using adb shell dumpsys power | grep mScreenOn
will work on devices that have a version number of 4.2+
The command that I have found to work on all devices I have tested so far is to use:
adb shell dumpsys input_method | grep mScreenOn
which will produce something like:
mSystemReady=true mScreenOn=true
which you can use to determine if the screen is on.
Tested on all Android Emulators in the range 2.2 - 4.4.2, Samsung Galaxy SII (4.0.4), Samsung Galaxy Tab 8.9 (4.0.4), and Nexus 4 with CM11
Also worth mentioning, on pre 4.2 devices you can use the command adb shell dumpsys power | grep mPowerState
to get something like this:
mIsPowered=true mPowerState=3 mScreenOffTime=24970 ms
mPowerState=SCREEN_BRIGHT_BIT SCREEN_ON_BIT
and detect if the SCREEN_ON_BIT string is present
how to check screen on/off status in onStop()?
You can try to use PowerManager system service for this purpose, here is example and official documentation (note this method was added in API level 7):
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
boolean isScreenOn = pm.isScreenOn();
EDIT:
isScreenOn() method is deprecated API level 21. You should use isInteractive instead:
boolean isScreenOn = pm.isInteractive();
http://developer.android.com/reference/android/os/PowerManager.html#isInteractive()
Screen on/off detection
Intent.ACTION_SCREEN_OFF and ACTION_SCREEN_ON check out for above broadcasts registration. here you can find a good example.
Detect Always-On Display when trying to determine if the screen is off
I found that the flags don't actually have anything to do with it. It seems that the display state will be one of several states including Display.STATE_DOZE_SUSPENDED
, Display.STATE_DOZE
and several others when the Always-On Display is active. So instead, I'm just checking for Display.STATE_ON
and treating all other states as the display being turned off.
public boolean isScreenOn() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
DisplayManager dm = (DisplayManager) getSystemService(Context.DISPLAY_SERVICE);
for (Display display : dm.getDisplays()) {
if (display.getState() == Display.STATE_ON) {
return true;
}
}
return false;
} else {
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
//noinspection deprecation
return pm.isScreenOn();
}
}
Check if screen is in MultiWindowMode without activity
Inside Fragment you can use
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
if (((Activity)getContext()).isInMultiWindowMode()){
// ...
}
}
How to check If I am currently on home screen
if (apps.get(0).topActivity.getPackageName().equals("com.android.launcher")
this somehow solved my problem only for default home
Getting if Screen ON/OFF and if Application is in Background/Foreground
There have been similar questions on Stackoverflow earlier. Here are links to a few of them:
How to determine if one of my activities is in the foreground
android:how to check if application is running in background
How can I tell if Android app is running in the foreground?
Related Topics
How to Take Multiple Photos Before Dismissing Camera Intent
Save State of Activity When Orientation Changes Android
Volley Out of Memory Error, Weird Allocation Attempt
How to Save the Image to Sd Card on Button Click Android
How to Get Current Buildtype in Android Gradle Configuration
Executing Multiple Asynctask's Parallely
Checking a Checkbox in Listview Makes Other Random Checkboxes Checked Too
How to Stop Other Apps Playing Music from My Current Activity
Taking Screenshot Programmatically Doesnt Capture the Contents of Surfaceview
Activate an Application When a Power Button Is Clicked
Crashlytics Android Sdk - Custom Uncaughtexceptionhandler
How to Correctly Pass Unique Extras to a Pending Intent
Open Android App from Url Using Intent-Filter Not Working
Error Type 3: Activity Class {...} Does Not Exist
How to Create a Number Picker Dialog