How to know whether the phone is locked mode? - android

Is there a way for me to know whether the phone is in locked state?

Have your app listen our for the ACTION_SCREEN_OFF broadcast. More information here.
public class ScreenReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
//screen locked
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
//screen unlocked
}
}
}
You might also want to receive information of when the user gets past the keyguard by registering for the ACTION_USER_PRESENT broadcast.

Related

how to detect if screen is locked if KeyGuard is not set - Android?

I want to know if screen is locked or not, when the SCREEN_OFF is broadcasted while user doesn't set KeyGuard?
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 :)

How to get alarm clock start ringing listener in Android?

I have a lock screen application in Android work well and now I want to improve it a bit.
When the alarm of the device start ringing my lock screen application must be finish. Please tell me how can I catch the alarm start ringing listener?
Thanks in advance.
You can set the BroadcastReceiver to listen to the alarm event. Upon receiving the relevant action you can stop you lock screen application.
Sample code will look like this,
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver()
{
#Override
public void onReceive(Context context, Intent intent)
{
String action = intent.getAction();
if (action.equals("com.android.deskclock.ALARM_ALERT") ||
action.equals("com.android.deskclock.ALARM_SNOOZE") ||
action.equals("com.android.deskclock.ALARM_DISMISS") ||
action.equals("com.android.deskclock.ALARM_DONE"))
{
// Stop the screen lock application here...
}
}
};

Get visibility of Lockscreen

How can I check from a Service if the KeyGuard (Lockscreen) is visible? I want to support the original and custom Lockscreens.
The screen locks only when the device turns the screen off.
You should extend BroadcastReceiver and implement onReceive, like this:
public class YourBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_SCREEN_OFF.equalsIgnoreCase(intent.getAction())) {
//screen has been switched off!
}
}
}
Then you just have to register it and you'll start receiving events when the screen is switched off:
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
appBroadcastReceiver = new AppBroadcastReceiver(yourActivity);
registerReceiver(appBroadcastReceiver, filter);
There is an edge case where users have their device set to lock n seconds after the screen goes off, you might want to add a check in your broadcast receiver for the ACTION_SCREEN_ON and check the time between them.

Screen on/off validate from android shell

I'm stuck at some validation for screen off and on test. I am using input keyevent 26 to put screen off and the same to wakeup. How to validate this test whether it was passed or failed. Is there any file where android write the state of the screen? any other way from dumpsys power? Can any one please suggest the way to check the state.
Thanks in advance.
You could write a simple app that has a broadcast receiver for the SCREEN ON and SCREEN OFF events, and log the events to the LogCat, and easily see it via adb logcat.
Here is some sample code for that. Make sure your app has been run at least once on the device, or it will not be registered for receiving the broadcast.
public class MyReceiver extends BroadcastReceiver {
private boolean SCREEN_ON = false;
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
SCREEN_ON = true;
Log.d(C.TAG, "Screen on");
}
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
SCREEN_ON = false;
Log.d(C.TAG, "Screen off");
}
}
}

Intent.ACTION_HEADSET_PLUG is received when activity starts

I am trying to pause music that is playing when the headset is unplugged.
I have created a BroadcastReceiver that listens for ACTION_HEADSET_PLUG intents and acts upon them when the state extra is 0 (for unplugged). My problem is that an ACTION_HEADSET_PLUG intent is received by my BroadcastReceiver whenever the activity is started. This is not the behavior that I would expect. I would expect the Intent to be fired only when the headset is plugged in or unplugged.
Is there a reason that the ACTION_HEADSET_PLUG Intent is caught immediately after registering a receiver with that IntentFilter? Is there a clear way that I can work with this issue?
I would assume that since the default music player implements similar functionality when the headset is unplugged that it would be possible.
What am I missing?
This is the registration code
registerReceiver(new HeadsetConnectionReceiver(),
new IntentFilter(Intent.ACTION_HEADSET_PLUG));
This is the definition of HeadsetConnectionReceiver
public class HeadsetConnectionReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Log.w(TAG, "ACTION_HEADSET_PLUG Intent received");
}
}
Thanks for the reply Jake. I should have updated the original post to indicate that I discovered the issue that I was having. After a bit of research, I discovered that the ACTION_HEADSET_PLUG Intent is broadcast using the sendStickyBroadcast method in Context.
Sticky Intents are held by the system after being broadcast. That Intent will be caught whenever a new BroadcastReceiver is registered to receive it. It is triggered immediately after registration containing the last updated value. In the case of the headset, this is useful to be able to determine that the headset is already plugged in when you first register your receiver.
This is the code that I used to receive the ACTION_HEADSET_PLUG Intent:
private boolean headsetConnected = false;
public void onReceive(Context context, Intent intent) {
if (intent.hasExtra("state")){
if (headsetConnected && intent.getIntExtra("state", 0) == 0){
headsetConnected = false;
if (isPlaying()){
stopStreaming();
}
} else if (!headsetConnected && intent.getIntExtra("state", 0) == 1){
headsetConnected = true;
}
}
}
I use a different approach to stop playback when headset is unplug. I do not want you to use it since you are already fine, but some other people may find it useful. If you get control of audio focus, then Android will send you an event audio becoming noisy, so if you write a receiver for this event it will look like
public void onReceive(Context context, Intent intent) {
if (AudioManager.ACTION_AUDIO_BECOMING_NOISY.equals(intent.getAction())) {
if (isPlaying()){
stopStreaming();
}
}
}
I ran into the same issue. I'm not sure what causes it, but at least in my testing it seems to be consistent, which means you can work around it. I did just that by adding a boolean member variable that starts as true, and is set to false on the first onReceive(Context, Intent) call. This flag then controls whether I actually process the unplug event or not.
For your reference, here is the code I use to do just that, which is available in context here.
private boolean isFirst;
public void onReceive(Context context, Intent intent)
{
if(!isFirst)
{
// Do stuff...
}
else
{
Log.d("Hearing Saver", "First run receieved.");
isFirst = false;
}
}

Categories

Resources