After use PowerManager my device never goes to sleep mode anymore - android

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();

Related

Android 13 Wake up Screen programmatically

Here is code working on Android 9. But on Android 13 (Samsung Galaxy) it doesn't work.
// Unlock screen & wake up phone
if ( deviceManger.isAdminActive( compName )) {
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();
}
Do you have any hints what should be modified for Android 13.
Thank you
I have tried but it also doesn't work
Window window = this.getWindow();
window.addFlags(LayoutParams.FLAG_DISMISS_KEYGUARD);
window.addFlags(LayoutParams.FLAG_SHOW_WHEN_LOCKED);
window.addFlags(LayoutParams.FLAG_TURN_SCREEN_ON);

How to Unlock Android Screen From inside Service (Marshmallow)?

I am developing an app to schedule whattsapp messages. Curruntly i want to unlock screen and start whattsapp intent and send msg inside my service . I searched here and tried following solution. but both are depreciated. Method 1 (depreciated) `
Window window = this.getWindow();
window.addFlags(LayoutParams.FLAG_DISMISS_KEYGUARD);
window.addFlags(LayoutParams.FLAG_SHOW_WHEN_LOCKED);
window.addFlags(LayoutParams.FLAG_TURN_SCREEN_ON);
Method 2 (depreciated)
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();
I tried both methods but none seems to work. both Root and Noroot methods will work
Please Help

Trigger SCREEN_ON event android through a background Service

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();
}
}

Android Wakelock not working when screen is off

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");

Android Trun on backlight

I need to turn on screen back-light and keyboard backlight when my application receive notification. I have tried with PowerManager It it wasn't successful.
Is there any way to turn on screen and keyboard backlights?
Thank You.
I have solved this issue.
private void backlightON(Context context){
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "MessageReader");
wakeLock.acquire();
}

Categories

Resources