How can I make SCREEN_ON event if my phone is in SCREEN_OFF.
Or in other words, how can I create some intent so that it invokes my phones screen from off to on.
Actually I have a service which creates a view over the top of lock screen.
In order to test it, I want to create a timer which invokes screen every 1 minute if screen is off. The service runs in background infinitely.
Try this:
private void screenOn() {
try {
PowerManager pm = (PowerManager)getSystemService(Context.POWER_SERVICE);
boolean isScreenOn;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
isScreenOn = pm.isInteractive();
}
else {
isScreenOn = pm.isScreenOn();
}
if(!isScreenOn){
KeyguardManager km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
KeyguardManager.KeyguardLock kl = km .newKeyguardLock("MyKeyguardLock");
kl.disableKeyguard();
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK |PowerManager.ACQUIRE_CAUSES_WAKEUP |PowerManager.ON_AFTER_RELEASE,"MyLock");
wl.acquire(10000);
// PowerManager.WakeLock wl_cpu = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,"MyCpuLock");
// wl_cpu.acquire(10000);
}
}catch (Exception e){
e.printStackTrace();
}
}
Related
I want my app to run in the background or after the phone has been put to sleep and when you near a certain latitude and longitude my app will immediately pop to the foreground. this must work no matter if it was from sleep or another app was in foreground.
I am using a service and a WakeLock. I am pretty sure the service is working but I don't know how to check because the debugger returns nothing if it is not in the foregrond. If it is working then my WakeLock is not working as anticipated.
public abstract class WakeLocker {
private static PowerManager.WakeLock fullWakeLock;
private static PowerManager.WakeLock partialWakeLock;
private static PowerManager pm;
public static void acquire(Context context) {
if (fullWakeLock != null) fullWakeLock.release();
if (partialWakeLock != null) partialWakeLock.release();
pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
fullWakeLock = pm.newWakeLock((PowerManager.SCREEN_BRIGHT_WAKE_LOCK |
PowerManager.FULL_WAKE_LOCK |
PowerManager.ACQUIRE_CAUSES_WAKEUP), "Loneworker - FULL WAKE LOCK");
partialWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Loneworker - PARTIAL WAKE LOCK");
partialWakeLock.acquire();
}
public static void release() {
if (fullWakeLock != null) {
fullWakeLock.release();
fullWakeLock = null;
}
if (partialWakeLock != null) {
partialWakeLock.release();
partialWakeLock = null;
}
}
public static void wakeDevice(Context context) {
fullWakeLock.acquire();
//KeyguardManager keyguardManager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
//KeyguardManager.KeyguardLock keyguardLock = keyguardManager.newKeyguardLock("TAG");
//keyguardLock.disableKeyguard();
}
public static boolean fullWakeLockActive() {
return fullWakeLock != null;
}
}
the fullWakeLock is being acquired here
if (makeUseOfNewLocation(location) < 20) {
if (WakeLocker.fullWakeLockActive())
WakeLocker.wakeDevice(allert.getMA());
if (callProgress.isOffHook()) {
new BlinkBack(allert.getMA());
}
The wake lock itself cannot wake the device from sleep or cause your so to come to the foreground. You would have to use location services or an alarm to wake the device up, then use your wake lock to keep the device awake and trigger a UI component of your app to the foreground.
I´m doing an app and I´m testing it on devices Samsung galaxy Y (Android 2.3.6) and Engel tablet 7" (Android 4.0.x).
In my app´s code I´m using this snippet in order to wake up the devices(when they are in sleep mode) and works well , but now my problem is that after wake up they never go to sleep mode anymore, only they go if I press the hardware button.
Anyone know how I can set the default energy settings after use PowerManager ?.
KeyguardManager km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
final KeyguardManager.KeyguardLock kl = km .newKeyguardLock("MyKeyguardLock");
kl.disableKeyguard();
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
WakeLock wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK
| PowerManager.ACQUIRE_CAUSES_WAKEUP
| PowerManager.ON_AFTER_RELEASE, "MyWakeLock");
wakeLock.acquire();
Try this:
void wakeUpGirls() {
if (wakeLock != null) {
wakeLock.release();
}
KeyguardManager km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
KeyguardLock keyguardLock = km.newKeyguardLock("TAG");
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
keyguardLock.disableKeyguard();
PowerManager pm = (PowerManager)getSystemService(Context.POWER_SERVICE);
wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK
| PowerManager.ACQUIRE_CAUSES_WAKEUP
| PowerManager.ON_AFTER_RELEASE
| PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "MyWakeLock");
wakeLock.acquire();
}
void sleepGirls() {
if (wakeLock != null) {
wakeLock.release();
wakeLock = null;
}
}
Use this one it works for me
PowerManager powermanager= ((PowerManager)getSystemService(Context.POWER_SERVICE));
WakeLock wakeLock = powermanager.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "tag");
wakeLock.acquire(9000);
wakeLock.release
KeyguardManager km =(KeyguardManager)getSystemService(KEYGUARD_SERVICE);
k1 = km.newKeyguardLock("IN");
k1.disableKeyguard();
I write application with service which periodically takes data from server,analyze them and after it launches alarm ( if it necessary), I use partial wake lock( in service) for awake device and parse json data, then I use full wake lock for awake screen at device. but it is doesn't work, how to solve this problem?
code:
Autobus66WakeLock.partialLockOff(Autobus66Service.this);
Autobus66WakeLock.lockOn(Autobus66Service.this);
after that this code returns false
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
return pm.isScreenOn();
lockOn method:
public static void lockOn(Context context) {
LocalLog.appendLog("full lock off");
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
//Object flags;
if (wl == null)
wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "MATH_ALARM");
wl.acquire();
}
lock off:
public static void partialLockOff(Context context){
LocalLog.appendLog("partial lock off");
if(pWlock != null && isPartialWakeLock){
pWlock.release();
isPartialWakeLock=false;
}
}
I m trying to implement wake lock and wifi lock for a service. More about my scenario in this like: Wakelock implementation in a service. I m acquiring during start and releasing when the service gets destroyed. I want to monitor the battery consumption for my project, though I understand that, this approach is not good. But when my phone screen is off, it is not working. I have enabled the option: Keep Wifi on during sleep. As soon as my screen turns off, my service stops.I m using the snippet :
PowerManager powerManager = (PowerManager)getSystemService(Context.POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Lock");
if(!wakeLock.isHeld())
{
wakeLock.acquire();
}
Log.d(TAG,"Acquired Wake lock successfully");
wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
wifiLock = wifiManager.createWifiLock(WifiManager.WIFI_MODE_FULL_HIGH_PERF , "WiFi LockTag");
if(!wifiLock.isHeld())
{
wifiLock.acquire();
}
Log.d(TAG,"Acquired Wifi lock successfully");
//TO RELEASE
try{
wifiLock.release();
}catch(Exception e){
e.printStackTrace();
Log.d(TAG,"Exception while releasing WakeLock :: Can be ignoredd");
}
Any idea of why this is happening ? Thanks in advance!
to protect screen from being lock use this Spinet
public static void unlockTheScreen(Context context) {
KeyguardManager keyguardManager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
KeyguardLock keyguardLock = keyguardManager.newKeyguardLock("TAG");
keyguardLock.disableKeyguard();
}
Add below in your wake lock
wakeLock = powermanager.newWakeLock((PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP ), "TAG");
For my application, I need to know that the screen is locked. How to check this is problematically. I used following flag:
if(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON != 0){
// some code
}else if((WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED)!= 0){
// some code
}
But this always executing both if and else part... which flag I have to use to check the screen is locked or not?
I'll try to answer this though the question is already old since it is unresolved and could help other googlers. ;)
First you must register a BroadcastReceiver for Intent.ACTION_SCREEN_OFF & Intent.ACTION_SCREEN_ON. Note that this receiver must be registered in codes and will not work when declared in the manifest.
In your broadcast receiver, when you receive Intent.ACTION_SCREEN_ON, you can check if the screen is locked by using the below codes:
KeyguardManager km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
boolean locked = km.inKeyguardRestrictedInputMode();
KeyguardManager myKeyManager = (KeyguardManager)getSystemService(Context.KEYGUARD_SERVICE);
if( myKeyManager.inKeyguardRestrictedInputMode()) {
//screen is locked
} else {
//screen is not locked
}
Register a broadcast receiver with action android.intent.action.ACTION_SCREEN_OFF and write your code in onReceive() method of receiver.
If you are using an activity, onPause() will be called when the screen locked and onResume() will be called when the screen unlocked.
In your code you are checking some flags, i don't know where you will do that checking ? is it continuous verification ? If you are using an activity in your app, the above procedure will happen, just check it in Android Developers website.
I guess you may have already found the answer, but if not (and for other developers), you can do it like this:
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
boolean isScreenOn = powerManager.isScreenOn();
if (!isScreenOn) {
//Screen is in OFF State
//Code to power on and release lock
KeyguardManager km = (KeyguardManager) this
.getSystemService(Context.KEYGUARD_SERVICE);
final KeyguardManager.KeyguardLock kl = km
.newKeyguardLock("MyKeyguardLock");
kl.disableKeyguard();
PowerManager pm = (PowerManager) this
.getSystemService(Context.POWER_SERVICE);
WakeLock wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK
| PowerManager.ACQUIRE_CAUSES_WAKEUP
| PowerManager.ON_AFTER_RELEASE, "MyWakeLock");
wakeLock.acquire();
}
There are broadcasted intents for screen lock & unlock.
Check it like :
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)){//LOGIC Here}
Let me know!
Here is what I did:
This handles if the user has unlocked the screen, but not yet entered the home screen or the user's screen is turned off say during a call.
if (Intent.ACTION_SCREEN_ON.equals(pIntent.getAction()) ||
Intent.ACTION_USER_PRESENT.equals(pIntent.getAction())) {
if(mListener!=null) {
KeyguardManager km = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
boolean locked = km.inKeyguardRestrictedInputMode();
Log.v(TAG, ": Phone lock state from KEYGUARD_SERVICE: Current state:" + (locked ? "LOCKED":"UNLOCKED"));
mIsPhoneLocked = locked;
}
}