I want to know if screen is locked or not, when the SCREEN_OFF
is broadcasted while user doesn't set KeyGuard
?
Asked
Active
Viewed 1,556 times
0

Ahmed Mostafa
- 918
- 1
- 12
- 20
1 Answers
0
1.Take a look in the KeyguardManager documentation here
2.Try this code
public class ScreenReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
// do whatever you need to do here
//screen is locked & check Keyguard is enabled or Not.
KeyguardManager keyguardManager = (KeyguardManager)getSystemService(Activity.KEYGUARD_SERVICE);
Log.v(TAG,""+keyguardManager.inKeyguardRestrictedInputMode());
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
// and do whatever you need to do here
}
}
}
Hope this helps.
Happy Coding :)

Community
- 1
- 1

Venkatesh Selvam
- 1,382
- 2
- 15
- 28
-
Thank you for help, but as I say in the question `KeyGuard` is not set ... so `inKeyguardRestrictedInputMode()` return `false` – Ahmed Mostafa Nov 04 '15 at 19:11
-
You didn't get it, what if the device isn't secured using `KeyGuard` , then using `KeyGuardManager` is absolutely useless. – Ahmed Mostafa Nov 05 '15 at 14:30