I would like my application to react to screen locking / putting phone into sleep. Based on some condition my application could decide to allow locking/sleep or to do something.
I want to use it that way:
User install my application (he don't have to run it all the time).
When phone want to go sleep or the screen is about to lock, my application receive the "message".
My application run some logic and decided either to allow lock/sleep or to perform some operation (that will prevent screen from lock this time).
If the user turn off the phone, my application can but don't have to react to it.
Is it possible to receive and react to screen locking and/or start of the sleep in order to prevent it (sometimes)?
Well, you can first let your app receive the ACTION_SCREEN_OFF intent, which would do:
When phone want to go sleep or the screen is about to lock, my application receive the "message".
http://developer.android.com/reference/android/content/Intent.html#ACTION_SCREEN_OFF
You can also use the PowerManager to wake the device up too, which
will prevent screen from lock this time
http://developer.android.com/reference/android/os/PowerManager.html
Related
I want to control my android smartphone that after my son playing games on my phone after a specified time such as half an hour, the phone will be locked unless you submit the right password.
I don't know how to trigger the lock screen process.
How can I implement interrupting the games processes and lock the cellphone?
How unlock and lock screen programatically in android Has some excellent information on immediately calling the lock screen. You could implement a system using an alarm manager to trigger this event.
Here is a good example,
first thing you need to do this, U need to know when the screen gets unlock. So here you go.....
android-detect-phone-unlock-event-screen-on
After that you set a time for 30/40/50 minutes (as you wish).
Once the timer fires, use below link to lock your screen.
lock-phone-screen-programmtically
And your work is done.
My app wakes the phone from standby and turns on the screen (SCREEN_BRIGHT_WAKE_LOCK). I can't use the WindowManager-flags approach, as my app may already be running in the background.
The problem is that once the user dismisses my app and the WakeLock is released, the screen turns off immediately, even if the user was interacting with the app (or the homescreen, which briefly shows while the app is being closed).
Would using the ON_AFTER_RELEASE-flag help?
Yes, you want to use the ON_AFTER_RELEASE flag.
When this wake lock is released, poke the user activity timer so the screen stays on for a little longer.
I want my app to keep running when the screen is blank and locked.
This is an instrumentation app that runs when the phone is in the pocket, so we can blank the screen to save power.
Currently:
If the phone locks automatically the screen goes blank and the app terminates. When the phone is unlocked the app needs to be re-started.
If the phone is manually locked (with a tap on the power button) the screen goes blank, the app stays running, but the data to voice output stops "working". When the phone is unlocked the app is still running and the data to voice starts "working" again.
I don't think WakeLock will do that for me. It's a function that's more like a sticky bit, as in load and don't terminate unless there is a specif command to terminate.
Any pointers to what I should be doing would be appreciated - thanks - Rob
Why don't you start a 'Service' that your Activity's can bind to. This can run even when your App isn't.
http://developer.android.com/reference/android/app/Service.html
A Service is an application component representing either an application's desire to perform a longer-running operation while not interacting with the user or to supply functionality for other applications to use.
Imagine how a mp3 playing application works, same scenario.
I'm developing an app which requires the system to get the touch events even after the system goes to sleep mode or after the user locks the screen, I tried searching for a solution but it is hard to find one.
Should I want to set any permissions or is there any inbuilt methods or can I override any methods to perform this functionality.
I'm developing an app which requires the system to get the touch events even after the system goes to sleep mode or after the user locks the screen
Fortunately, this is not possible. Otherwise, the device would not be asleep, and battery life would suffer as a result.
Here is a link that shows how to prevent the phone from sleeping.
If you couple that with, say a black screen to 'pretend' the hone is sleeping but actually running your code. So your code can still intercept touch event
Then you need to install your app as a service and make it start when the device is turned on.
You will not need NDK or rooted device for that (sorry, got a short night :) )
I am writing an Android alarm-like app.
I want to let the user to choose if to always keep the screen on for the whole application duration, or if she want it to go to sleep according to her device power manager settings.
The first scenario is correctly handled with this code:
getWindow().addFlags(LayoutParams.FLAG_KEEP_SCREEN_ON);
But - in the second scenario - when the screen has been swichted off by power manager, I can play a sound via AudioManager, but I can't force the screen to switch on...
I'm using Build.VERSION.SDK = 10 and testing on a Samsung device with Android 2.3.4.
You can maintain a WakeLock, and acquire the WakeLock when you want to turn the screen on.
You can check out WakeLock here.