Get visibility of Lockscreen - android

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.

Related

Log Android user wakeup/unlock

I'm trying to create an application that has a constant running service which will log and display to the user everytime they unlock or change the phones state from screen off to screen on.
Is this possible without using the Log cat through the P.C?
Yes you can do it using Broadcast Receiver. Android system sends always broadcasts for specified changes. You can handle this broadcasts using Broadcast Receiver.
Example:
We will create a new Broadcast Receiver to handle the Screen On and Screen Off state:
public class PhoneStateBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
//we will handle here the activities which will tell us that
// the screen state has changed
if(intent.getAction().equals(Intent.ACTION_SCREEN_OFF)){
//write log that the screen is off
}else if(intent.getAction().equals(Intent.ACTION_SCREEN_ON)){
//write log that the screen is on
}
}
}
All what you need is to declare this BroadcastReceiver in Android Manifest.xml
file:
<receiver android:name="yourpackage.PhoneStateBroadcastReceiver">
<intent-filter>
</intent-filter>
</receiver>
And after that you should start your PhoneStateBroadcastReceiver on yout
Service onCreate Method:
//...
public void onCreate(){
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_SCREEN_ON);
PhoneStateBroadcastReceiver pSReciever = new PhoneStateBroadcastReceiver();
registerReceiver(pSReciever, filter);
}

Broadcast listener to user touch in Android

I want to know the time left before screen off.
I can easily get the timeout defined by system, but the counter resets with each touch the user do. so I need to reset the counter on my side as well.
I don't want to have an activity in foreground but to use (probably) a broadcastReceiver.
You can't do this but you can detect when screen off and screen on - Use actions:
Intent.ACTION_SCREEN_ON
Intent.ACTION_SCREEN_OFF
But do not put it in AndroidManifest.xml you must register this broadcast, example:
public class ScreenReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent){
String action = intent.getAction();
if(action.equals(Intent.ACTION_SCREEN_ON)){
// screen on
} else {
// screen off
}
}
}
To register broadcast (do not call it in this broadcast (ScreenReceiver) - call it in Service, Activity or Application):
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
registerReceiver(new ScreenReceiver(), filter);
I guess the best solution is register broadcast in service because when Activity calls onDestroy() broadcast may be automatically unregistered.

android broadcastreceiver when screen off or lock?

I have broadcastreceiver in my application and it's work good, when i connect with the smart phone to bluetooth device it's show me a alert dialog.
But, if the screen is off or in the lock screen, it's not show me the alert dialog, and i want to show the alert dialog even if the smartphone on the lock screen or the screen off.
How i can do this?
Thanks!
public class ScreenReceiver extends BroadcastReceiver {
public static boolean wasScreenOn = true;
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
//do something here
}
}
}
This is how you register the receiver.
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
BroadcastReceiver mReceiver = new ScreenReceiver();
registerReceiver(mReceiver, filter);
Since you didn't provide any code, I cannot be sure about this. However, your problem might be related to the context from which you are declaring the Intent (if any), or anyway the dialog. Try to use context.getApplicationContext() instead of context / this:
final Intent dialogIntent = new Intent(context.getApplicationContext(), DialogActivity.class);
context.startActivity(dialogIntent);

How to reveal that screen is locked?

In my application I need to know when device is locked (on HTC's it looks like short press on "power" button). So the question is: which event is triggered when device is locked? Or device is going to sleep?
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 a better way:
KeyguardManager myKM = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
if( myKM.inKeyguardRestrictedInputMode()) {
//it is locked
} else {
//it is not locked
}
In addition to the above answer, in-case you want to trigger some action when your app is at the foreground:
You could use the event called onResume() to trigger your own function when your app takes the spotlight from a previously resting state, i.e, if your app was at the background(paused/minimized...)
protected void onResume()
{
super.onResume();
//call user-defined function here
}

How to stop service when ACTION_SCREEN_OFF

I am trying to make my UpdateService for my digital clock widget stop when the screen is turned off to conserve battery, and then back on when the screen is activated. I currently have it in my onReceive() in my AppWidgetProvider, but I have also tried it in a BroadcastReciever.
My current code is:
public static boolean wasScreenOn = true;
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
Log.d("Screen switched on. ", "Starting DigiClock UpdateService.");
context.startService(new Intent(UpdateService2by2.ACTION_UPDATE));
wasScreenOn = false;
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
Log.d("Screen switched off. ", "Stopping DigiClock UpdateService.");
context.stopService(new Intent(context, UpdateService2by2.class));
wasScreenOn = true;
}
Can anyone help me out here? I am stumped.
I'm fairly sure that you have to register your receiver in code for ACTION_SCREEN_OFF/ON. I don't think registering them in the manifest will work.
It seems you cannot register for the ACTION_SCREEN_ON/OFF intents with a filter in the manifest. You have to register your BroadcastReceiver in code. See here for an examples for an activity and a service.
You'll only receive the intent if your service or activity is running. In contrast to other broadcast events, the system will not start your process to handle the SCREEN_ON intent. It is similar to ACTION_BATTERY_CHANGED in this regard.
To handle this intent with a widget, I think you have to start a service that listens for the intent and then notifies your widget.
The article linked from Jens' answer to this same question provides a great presentation on this topic. I used it to implement a solution that worked for me.
The key insight is that your BroadcastReceiver can only be registered in code; you neither want nor need an entry in your manifest.
I recommend the article, but for those in a hurry, the simplest possible functioning approach would be to just paste something like this into your activity's onCreate() method:
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
BroadcastReceiver screenoffReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
Log.v("screenoffReceiver", "SCREEN OFF");
}
else if(intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
Log.v("screenoffReceiver", "SCREEN ON");
}
return;
}
};
registerReceiver(screenoffReceiver, filter);
As the article points out, for a running activity, onPause() / onResume() are always called when the power button is used to blank/unblank the display, so if you have something that you don't mind doing even in those cases for which the power button was not the reason for the onPause() or onResume() call, you can do it in those methods and thereby catch every instance of the power button being used to blank or unblank the screen (along with calls due to other causes), without the overhead of creating a BroadcastReceiver.
That was the case for me; I had a button that made my activity's main view invisible when pressed and visible when released, and if the user held the button down and then tapped the power button while the app was hidden, it would stay hidden permanently when the screen was unblanked by another tap of the power button. So in my case I only had to put a setVisibility(VISIBLE) call for my main view in my activity's onResume() override.
In fact, as the article shows, even if you only want to respond to the power button events, the proper way to do that is to set a flag in the BroadcastReceiver and then test that flag in onPause() and/or onResume() to see if the power button was the specific cause of the call to those methods.
Maybe you should use
public class MyReceiver extends BroadcastReceiver {..}
And then use this classname in the manifest

Categories

Resources