Android Trun on backlight - android

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

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

Switch between wakelocks

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

Android Turn screen Off

I can't turn off the screen using this code. I used PowerManager and wl.release() method, but it doesn't work. Can somebody show me an example?
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "DoNotDimScreen");
This is part of my function:
stateString = "nextone";
if(stateString=="nextone"){
wl.release();
}
I also added permission in the manifest but no result.
I found the answer over here on stack overflow: Turn off screen on Android
Copied from there:
WindowManager.LayoutParams params = getWindow().getAttributes();
params.flags |= LayoutParams.FLAG_KEEP_SCREEN_ON;
params.screenBrightness = 0;
getWindow().setAttributes(params);
I tried this out and it seems to work.
If you don't use a permission, the program will crash with a SecurityException when it tries to lock, so that isn't the problem. The correct method is: (obtains WakeLock on start, gives it up when the application loses focus (onPause)
//declared globally so both functions can access this
public PowerManager.WakeLock wl;
///////////onCreate
//stop phone from sleeping
PowerManager powman = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = powman.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "NameOfLock");
wl.acquire();
///////////onPause
wl.release();
//////////for completion's sake, onResume
if(!wl.isHeld()){
wl.acquire();
}
However, your problem is actually in this check
if(stateString=="nextone")
This should be if(stateString.equals("nextone"))
please check this link before proceeding with wake lock. if it does not solve your problem then you can proceed with wake lock.
Force Screen On
You can use
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
try
{
Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, 1000*15);
}
catch (NumberFormatException e)
{
Log.e("aa", "could not persist screen timeout setting", e);
}
How to detect switching between user and device

Turning on screen programmatically

I would like to unlock screen and switching it on to show a popup on an event trigger. I am able to unlock the screen using
newKeyguardLock = km.newKeyguardLock(HANDSFREE);
newKeyguardLock.disableKeyguard();
on KeyGuardService but I cannot turn on the screen. I am using
wl = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, HANDSFREE);
wl.acquire();
but with no success. The screen still remains off.
How can I achieve this?
Note from author: I wrote this back in 2012. I don't know if it works anymore. Be sure to check out the other more recent answers.
Amir's answer got me close, but you need the ACQUIRE_CAUSES_WAKEUP flag at least (Building against Android 2.3.3).
WakeLock screenLock = ((PowerManager)getSystemService(POWER_SERVICE)).newWakeLock(
PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG");
screenLock.acquire();
//later
screenLock.release();
This is very popular question but the accepted answer now is outdated.
Below is latest way to Turn On Screen OR wake up your device screen from an activity:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
this.setTurnScreenOn(true);
} else {
final Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
}
Use WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON but FLAG_TURN_SCREEN_ON flag has been deprecated in API level 27 so you can use Activity.setTurnScreenOn(true) from API level 27 onward.
In your main activity's OnCreate() write following code:
((PowerManager)getSystemService(POWER_SERVICE)).newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "TAG").acquire();
It causes the device to wake up.
Do not forget disableKeyguard() to unlock the device...
undefined's answer with NullPointer check and set timeout :
private void turnOnScreen() {
PowerManager.WakeLock screenLock = null;
if ((getSystemService(POWER_SERVICE)) != null) {
screenLock = ((PowerManager)getSystemService(POWER_SERVICE)).newWakeLock(
PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG");
screenLock.acquire(10*60*1000L /*10 minutes*/);
screenLock.release();
}
}
This is how you can do it:
PowerManager powerManager = (PowerManager) context.getSystemService(context.POWER_SERVICE);
PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK |
PowerManager.ACQUIRE_CAUSES_WAKEUP |
PowerManager.ON_AFTER_RELEASE, "appname::WakeLock");
//acquire will turn on the display
wakeLock.acquire();
make sure to set permission in the manifest :
<uses-permission android:name="android.permission.WAKE_LOCK"/>

Categories

Resources