I want to create an alert application. The alert is received using GCM (GoogleCloudMessaging). Now I want to show the alert message on the screen without any actions by the user. Therefore I need to wake up the device and unlock the screen.
In the forum I found this answer (and some similar) describing how I can achieve this behaviour:
android-wake-up-and-unlock-device
Reading the documentation, using WakeLocks seems to be deprecated: i.e.
full_wake_lock
FULL_WAKE_LOCK Added in API level 1
int FULL_WAKE_LOCK
This constant was deprecated in API level 17. Most applications should
use FLAG_KEEP_SCREEN_ON instead of this type of wake lock, as it will
be correctly managed by the platform as the user moves between
applications and doesn't require a special permission.
In the documentation an alternative is shown using the WindowManager.
With help of the flags
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
I should be able to bring my alert message to the front without asking for the keyguard secure pattern.
My question now is:
When I want to use these flags, do I also need a wakeLock to wake up my device or is this done implicit by the window manager?
Related
I have implemented a call recognition app which displays a View that contains the caller id. More specifically it is a Service with a WindowManager where I add the View. I also use the draw over other apps permission.
The problem is only in Android 8, in case that the user enables the screen lock (Settings-> Security & Location -> Screen Lock).
In this senario if the device screen is off, and someone calls, my call recognition is not displayed. (The only thing I can see is the native call recognition app that is ringing)
My Service code runs correctly but the user cannot see the UI.
Do you have something in mind?
Thank you in advance
After several tries I found that the following parameter should be passed in the WindowManager.LayoutParams constructor.
flags -> WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
For earlier versions of Android although I didn't use this parameter it was ok.
The goal of my app is to keep the screen on across the whole Android system. Previously, I've used FULL_WAKE_LOCK for this and it allowed me to block dimming of the screen across the system. However, since the API Level 17, it got deprecated:
This constant was deprecated in API level 17. Most applications should
use FLAG_KEEP_SCREEN_ON instead of this type of wake lock, as it will
be correctly managed by the platform as the user moves between
applications and doesn't require a special permission.
Official documentation recommends using FLAG_KEEP_SCREEN_ON, however it is possible to use it only for particular Activity.
I would still FULL_WAKE_LOCK, however I've found that it doesn't work now on some of the devices, like MediaPad Huawei x2, Redmi Note 3, etc. The way I'm currently using the PowerManager can be found on GitHub. Is there any better way to do accomplish this task after API level 17?
Starting with API 23 and its new Doze mode Wake Locks are ignored and they do not prevent the system from entering sleep.
You should experiment with maintaining a foreground service in parallel with the wake lock, theoretically that should prevent the device from entering sleep.
NOTE: the foreground Service has to call startForeground and show a non dismissable Notification
Is it possible to create an app on iphone and android that is accessible before the authentication screen, just like the "emergency dialler".
I searched on google but I cant seem to find relevant information.
On Android perhaps, depends on what you want exactly. You can make an app that appears above the keyguard by specifying FLAG_SHOW_WHEN_LOCKED. You can make it turn on the display with a wakelock or the flag FLAG_KEEP_SCREEN_ON. You can make it dismiss the keyguard with FLAG_DISMISS_KEYGUARD . But some of these will only work with a non-secure keyguard (one that doesn't require a password).
I am working on an application that will replace the default lock screen (swipe to unlock) for android devices. I have successfully done this by disabling the keyguard manager and showing my activity using the broadcast receiver for screen OFF and screen ON intent. Now, the problem is when I set the default screen lock again for any reason then my application would not disable the keyguard unless I force close it and launch it again.
km = (KeyguardManager) getApplicationContext().getSystemService(Context.KEYGUARD_SERVICE);
if( km.inKeyguardRestrictedInputMode()) {
//it is locked
km = (KeyguardManager) getApplicationContext().getSystemService(KEYGUARD_SERVICE);
kl=km.newKeyguardLock("com.example.helloworld.MainActivity");
kl.disableKeyguard();
} else {
Intent i = getIntent();
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplicationContext().startActivity(i);
}
You cannot replace the lock screen with a user application. Anything you do is a hack and may or may not work on some device, and will likely break with new releases. You can create something that looks like a screen lock, but it won't work like one. Additionally, in recent versions of Android (post-ICS), unlocking the screen does extra things like unlocking the credential storage, which your app cannot possibly do (since it doesn't have system permissions).
If you really want to replace the screen lock, you need to build your own Android ROM, modifying/replacing the stock one.
The accepted answer may be out of date.
It's now possible to use Device Admin to create and remove device passwords.
An application can be placed above the current lock screen using FLAG_SHOW_WHEN_LOCKED (complete explanation in another answer)
The keyguard can be dismissed using FLAG_DISMISS_KEYGUARD
As a result, it's theoretically possible to secure the actual lock screen using an app-generated password (providing real security), float a custom lock screen above the android lock screen, and -- when a proper password is provided -- unlock and dismiss the real lock screen. Finally, you would use a receiver to restore or clear the password on relevant events like SCREEN_OFF or SCREEN_ON -- the latter could automatically clear the password if a timeout was not yet reached.
FWIW, I don't recommend this approach since a crash or uninstall would leave a user with a device locked by a password they do not know.
I have a Service that keeps the display on at a dim level at certain times, and it uses a 'dim' WakeLock to accomplish this. It works well... except that the screen never locks. That is, while the dim WL is held, the lock screen never appears requiring the user to swipe and authenticate.
Note that I'm developing on a platform that may have vendor changes to the low-level Android Java framework code, so this might not be standard Android behavior. But also, I have access to the framework code and can change it, if necessary. I just can't figure out where this policy is enforced in the code.
When the device is on external power, we want to keep the screen contents visible - but we still want it to lock.
The only way I can think of to do that is for you to maintain your own timer for when to trigger the lock, then to use DevicePolicyManager and lockNow() to lock the device at that point. This requires extra permissions and extra setup work (enabling your app as a device manager).