I made an field guide app for biology. It does tons of stuff, for example, recordings of wildlife sounds, and it is made to run the whole day with a single battery charge.
Since it is intended to be run a whole day, I cannot keep the screen on all the time. So naturally the screen goes off. Then, the lock screen kicks in, blocking the app. Then you need to enter the pin/pattern/whatever and you then miss the opportunity of a precious recording. Dozens of times per day. And in general the user is using my app in the wild, where there is much less risk of theft.
So I present the user an option in the preference section of my app to turn lock screen off while using it. There is no problem with this (see below for the code I made), except that when I switch to a secondary activity the lock screen appears. It is not truly a "lock screen", in the sense that it shows the back button that when you press it the lock screen disappears. But still, a pain when you're in a hurry. I want no lock screen. At all.
Interestingly, when I switch back to the primary activity from a secondary, no lock screen is shown...
This is the way I found to (partially) disable the lock screen (executed in each activity act):
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
act.setShowWhenLocked(true);
act.setTurnScreenOn(true);
((KeyguardManager) act.getSystemService(Context.KEYGUARD_SERVICE)).requestDismissKeyguard(act, null);
//if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)
// act.setInheritShowWhenLocked(true); // makes no difference?
} else {
Window window = act.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
}
Of course, I also have in the Manifest:
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
It turns out that the requestDismissKeyguard was causing the Lock Screen to be activated instead of deactivated (??!!). So I removed the line
((KeyguardManager) act.getSystemService(Context.KEYGUARD_SERVICE)).requestDismissKeyguard(act, null);
and now everything works just fine.
So I am fairly new to the world of coding and I am doodling with a little private learning project.
I have made a simple web browser based on WebView for a embedded android 7.1 ELOtouch device.
I have found plenty of articles online on how to turn the screen on/off etc but never really managed to make it work.
What I am trying to do is that the screen dims down to the lowest level after xx amount of time, let’s say 5min. And only dims back up to a defined level upon user touch/screen input.
The unit is always on and don’t have any form for advanced screen adjustments in settings, so as I see it, it has to be done programmatically.
Thankful for and advice or guidance.
Attached one of my sources:
How to change screen timeout programmatically?
To change screen brightness by user touch, you could do like this:
In AndroidManifest.xml file, add this line:
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
And in some place of activity class file:
Settings.System.putInt(this.getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS, 80);
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness =0.8f;
getWindow().setAttributes(lp);
startActivity(new Intent(this,DummyActivity.class));
Note: when setting up brightness,the modification doesn't take effect immediately, to solve this problem,just start another blank dummy activity and finish it in Oncreate()
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
finish();
}
Don' forget to include DummyActivity in AndroidManifest.xml.
Edited:
To dim screen after some time, the basic logic is same, and maybe you should create a Timer(TimerTask).
Hope this is helpful!
I am building a VoIP app and it has an incoming call screen. Instead showing the screen, I want to just show a heads up notification, if current foreground app is in full screen mode. Is this possible? Is there a way to check if the current running activity is in full screen?
WindowManager.LayoutParams lp = getWindow().getAttributes();
if(lp.flags == WindowManager.LayoutParams.FLAG_FULLSCREEN){
//Do your stuff
}
Edit
If the running app is not your app you need to take a different approach which possible only from API 11, and use View.OnSystemUiVisibilityChangeListener:
Callback to be invoked when the status bar changes visibility. This reports global changes to the system UI state, not what the application is requesting
I have an app which when the device locks it starts an activity instead. By doing this the device doesn't lock but it shows an activity. This is OK. My problem is that if I am already in the activity and when I press the power button the device locks.
In the BroadcastReceiver I can see the screen is off:
if (action.equals(Intent.ACTION_SCREEN_OFF)) {
isScreenOff = true;
final Intent serviceIntent = new Intent(context, Screensaver.class);
serviceIntent.putExtra("screen_state", isScreenOff);
context.startService(serviceIntent);
Log.i(TAG, "SCREEN OFF.");
}
Then the onStartCommand() in my service is called. In it I start the activity. In the activity I see that the onResume() is called and then onPause() and that's it. The device is locked and the screen is off. If I unlock it manually, the activity is there, but it shouldn't be necessary to unlock it manually. Do you have any idea how I can fix that? (Do not hesitate to ask me if I need to post more code).
EDIT
Here is a video to get it more clear. In the end of it I am in my screensaver activity and when I press the power button the device locks and does not starts the screen again.
I figured it out! Since the activity started when I lock the device, calling Activity.recreate() in onStop() fixed my problem.
The Context
Recently, I have been looking for reliable ways to control a secured Android Keyguard. Mainly to display a custom lock screen. I know that Google had stated custom lock screens are not officially supported by the platform and should expect things to break, however, with the existing APIs, I believe there must be ways to do this. I have done tons of research for about a week but still having problem here and there. What I have implemented, assuming a secured Keyguard is enabled, so far are,
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED allows an activity(a window) to be displayed on screen on, putting the Keyguard behind, and all unsafe actions are prevented. Notification panel is disabled, finishing the activity will bring up the Keyguard. I implemented as following in my lock screen activity.
#Override
public void onAttachedToWindow() {
window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
}
KeyguardManager, KeyguardManager.KeyguardLock are deprecated classes, but they still work all the way to Jelly Bean. To do this, I have a Service that handles two things, holding a static KeyguardManager and the related objects, and have it hold a BroadcastReceiver to receive Intent.ACTION_SCREEN_ON and Intent.ACTION_SCREEN_OFF. (all the objects are initialized properly)
For ScreenReceiver
public static synchronized void disableKeyguard() {
if ( isLocked ) {
if ( keyguardLock == null ) {
keyguardLock = keyguardManager.newKeyguardLock(LOG_TAG);
}
keyguardLock.disableKeyguard();
isLocked = false;
}
}
public static synchronized void reenableKeyguard() {
if ( !isLocked ) {
if ( keyguardLock == null ) {
keyguardLock = keyguardManager.newKeyguardLock(LOG_TAG);
}
keyguardLock.reenableKeyguard();
keyguardLock = null;
isLocked = true;
}
}
For BroadcastReceiver
#Override
public void onReceive( Context context, Intent intent ) {
if ( intent.getAction().equals(Intent.ACTION_SCREEN_ON) ) {
Intent start = new Intent(context, LockScreen.class);
start.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
context.startActivity(start);
} else if ( intent.getAction().equals(Intent.ACTION_SCREEN_OFF) ) {
ScreenReceiverService.reenableKeyguard();
}
}
For LockScreenActivity, when the user had input the correct passcode,
window.clearFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
ScreenReceiverService.disableKeyguard();
finish();
The Problem
Things that works
ACTION_ON and ACTION_OFF are received reliably.
LockScreenActivity is shown before the Keyguard (without telephone state handling yet)
Notification cannot be pulled down, exiting the activity in any way would display the lockscreen.
Things that does not work
After I disable Keyguard and call finish(), my app exits and homescreen or the last activity before the screen went off is shown. However, whenever I press the Home Key, the Keyguard will flash into the screen, quickly dismissing itself immediately, and the normal Home Key function/event is not handled (will not return to homescreen after flashing). This is observed when I rapidly tapped the Home Key repeatedly.
I even looked into the Android source code to find out the Home Key handling, but it is never sent to third-party applications unless the window type is WindowManager.LayoutParams.TYPE_KEYGUARD or WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG, which will throw SecurityException on 4.0+ even it worked on earlier platforms. And for the Keyguard, I have declared DISABLE_KEYGUARD permission use this shouldn't be the problem. My guess is the flag FLAG_SHOW_WHEN_LOCKED will tell the system to handle to Keyguard in some ways that would conflict with other disable calls. Since this flag is mostly used for Alarm/SMS type application, which is to show limited information to the user, then dismiss themselves and bring up the Keyguard. But in my case, having the user unlock my lock screen then unlock the system lockscreen simply defeats the purpose of my app.
So the question is why would the Keyguard flashes whenever I press Home after I disabled it? Is there any workaround/solution for this issue?
P.S. Thank you for reading such a long question. This is my first time asking a question here, if there is anything that I did wrong, please tell me (i.e. format, grammar, code convention, tags, etc.). Also I had no experience with any programming knowledge, I started with Android before I know what Java is. So I have not taken any proper course/training yet, this community is awesome and often help people like I even if they are simple questions, and of course watching Google I/O videos, reading blogs, read others' code help me a lot. So please tolerate any dumb mistakes/obvious bugs/stupid questions. I am only 16. ^_^"
I have used this with some success in both Gingerbread and ICS to open my activity (via a background service which is starting it). In the activity being started:
#Override
public void onAttachedToWindow() {
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN |
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON,
WindowManager.LayoutParams.FLAG_FULLSCREEN |
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
}
I had the same problem for the click of HOME button while unlocking the device. This can be solved by reseting the password to blank (ie "") :
DevicePolicyManager devicePolicyManager;
ComponentName demoDeviceAdmin;
devicePolicyManager.setPasswordQuality(demoDeviceAdmin,DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED);
devicePolicyManager.setPasswordMinimumLength(demoDeviceAdmin, 0);
devicePolicyManager.resetPassword("",DevicePolicyManager.RESET_PASSWORD_REQUIRE_ENTRY);
and then disabling the keygaurd :
this.keyGuardLock = ((KeyguardManager)getSystemService("keyguard")).newKeyguardLock("keyguard");
keyGuardLock.disableKeyguard();
Hope this solved your problem. \m/ keep coding!
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED allows an activity(a
window) to be displayed on screen on, putting the Keyguard behind
I tried to get this but my activity always preceded by the system lock screen. isOrderdBroadcast() says that ACTION_SCREEN_NO is an ordered broadcast.
I added flag to the activity :
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
in onAttachedView(). But still the system lock is getting the preference over my Custom screen lock activity.
How did you get your activity before the system lock screen?
EDIT
On a hindsight, I think my understanding of the lock screen concept was wrong. My broadcast receiver was getting the broadcast first. But what was showing before that was the system lock screen launched when SCREEN_OFF is received. Fixed that problem as of now.
But stumped by the ambiguity of home button behavior. This won't be a problem in post ICS devices as all hard buttons are discouraged.
In your LockScreenActivity, ending the validation code by finish(); kills the LockscreenActivity and thus the whole app. Instead of that, you could just launch back your main activity (or any other) like this :
startActivity(new Intent(LockScreenActivity.this, MainActivity.class));
If AOSP is in your control then you need to set the simple flag and keyguard() is gone for good.
Here is the details to do that, get into the file
"overlay/frameworks/base/packages/SystemUI/res/values/config.xml"
and search for "config_enableKeyguardService" then set the flag to false.
NO MORE keyGuard, pheww