I'm trying to turn the screen on when the app sends a notification. Currently when I acquire a WakeLock it flashes on and off very quickly, almost un-noticeably. As far as I can tell I have my flags set correctly. AQUIRE_CAUSES_WAKEUP to turn the screen on and ON_AFTER_RELEASE to (poke the user activity timer) leave it on once I release it - otherwise, I understand it turning itself off immediately... Anyone got an insight?
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE, "TAG");
if(mTurnScreenOn) {
wl.acquire();
Log.d("TAG", "WakeLock Acquired");
if(pm.isScreenOn()) { Log.d("TAG", "Screen is on");
} else { Log.d("TAG", "Screen is off"); }
}
mNotificationManager.notify(1, notification);
if(mTurnScreenOn) { //release the wakelock if needed
wl.release();
Log.d("TAG", "WakeLock Released");
if(pm.isScreenOn()) { Log.d("TAG", "Screen is on");
} else { Log.d("TAG", "Screen is off"); }
}
where you are setting the value of mTurnScreenOn , Look at your code you are acquiring wake lock when mTurnScreenOn==true and on same condition your are releasing it also.
for more info see this
Related
I want to set the wakelock time to the "unlimited" time or at least set the time to xx minutes / hours.
If I try these code :
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag");
wl.acquire();
It just give me 30 seconds for the wakelock. But if I change my code to wl.acquire(10*60*1000L /10 minutes/); as suggested from Android Studio, it didn't give any change to the wakelock time, any idea of it ?
Make sure you have the Manifest Permissions tag
<uses-permission android:name="android.permission.WAKE_LOCK" />
make sure to call wakeLock.release() when leaving the activity
#Override
protected void onDestroy() {
wakeLock.release();
super.onDestroy();
}
Wakelock doesn't exactly mean it will keep your screen on.
if you want to keep the screen on :
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);
}
then when you want to turn it off :
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
this.setTurnScreenOn(false);
} else {
final Window window = getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
}
I am trying to show a pop-up on the lock-screen. And also after a interval of time the screen have to go to sleep. It is working with acquiring the wakelock and releasing it after a period of time on a Activity. But the problem is if the screen is touched when showing the popup, then the screen is not sleeping. Here is the code that i have used.
mPowerManager = (PowerManager) getSystemService(POWER_SERVICE);
wl = mPowerManager.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "tag");
wl.acquire();
And for releasing wakelock after a period .
new Handler().postDelayed(touchTimer, 1000 * 12);
Runnable touchTimer = new Runnable() {
#Override
public void run() {
if(wl.isHeld())
wl.release();
}
};
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");
I have made an app that wakes up the screen when it receives a text message. I came up with the following code to wake the screen.
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
boolean isScreenOn = pm.isScreenOn();
if(!isScreenOn ){
final PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My tag");
wl.acquire();
Toast.makeText(getBaseContext(), "This is WAKEUP SCREEN", Toast.LENGTH_SHORT).show();
Thread timer = new Thread(){
public void run(){
try {
sleep(5000);
} catch (InterruptedException e) {
// TODO: handle exception
}finally{
wl.release();
}
}
};
timer.start();
}
Now the problem is that if I comment out or remove the statement Toast.makeText(getBaseContext(), "This is WAKEUP SCREEN", Toast.LENGTH_SHORT).show(); my screen wont wakeup. I am not sure what is the problem here... and I am using android 2.3.
Use PowerManager.SCREEN_DIM_WAKE_LOCK|PowerManager.ACQUIRE_CAUSES_WAKEUP instead of just PowerManager.SCREEN_DIM_WAKE_LOCK, and see if that helps.