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();
}
};
Related
My service starts an Activity like this:
Intent intent = new Intent(context, CallMonitor.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
this happens in CallMonitor.onCreate():
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
On most devices, the activity (called up from service) wakes up the device, turns on the screen and is displayed.
But - for example - on Galaxy Tab 4, the activity is only called if the screen is already switched on.
If the screen is switched off and the service calls up the activity, it is displayed with a delay - It will be displayed immediatly after turning the screen on.
There is also a voice output in the activity. When the Galaxy S4 is switched off, it will not be played back - but immediatly after turning the screen on again.
Any suggestions?
I don't want to use WakeLock!
It looks like that's a device limitation and I don't know how you could work around that without using a WakeLock. We have a really old piece of code which you could modify to your needs which pretty much always worked for us:
public static void bringToFront() {
try {
PowerManager pm = (PowerManager) MainActivity.getAppContext().getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE, "ALARM");
wl.acquire();
KeyguardManager keyguardManager = (KeyguardManager) MainActivity.getAppContext().getSystemService(Activity.KEYGUARD_SERVICE);
KeyguardManager.KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
lock.disableKeyguard();
if (MainActivity.getAppActivity() != null) {
MainActivity.getAppActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
}
} catch(Exception e){
e.printStackTrace();
Log.w(TAG, "bringToFront Err: "+e.toString());
}
}
I have written code to turn screen on/off based on the brightness. I am able to turn screen off but when screen is turned on the brightness is updated to 1 but screen doesn't wakes up and shows itself. I have to use the manual lock/unlock button on the device.
Is there something missing from my code? I am using Android 2.3.4
if (command.equals("ON")) {
runOnUiThread(new Runnable() {
#Override
public void run() {
onResume();
WindowManager.LayoutParams screenBrightness = getWindow()
.getAttributes();
screenBrightness.screenBrightness = 1;
screenBrightness.flags |= WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON;
screenBrightness.flags |= WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD;
screenBrightness.flags |= WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;
getWindow().setAttributes(screenBrightness);
}
});
WMLP = getWindow().getAttributes();
System.out.println("Screen Brightness ON: "
+ WMLP.screenBrightness);
} else if (command.equals("OFF")) {
runOnUiThread(new Runnable() {
#Override
public void run() {
WindowManager.LayoutParams screenBrightness = getWindow()
.getAttributes();
screenBrightness.screenBrightness = 0;
screenBrightness.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
getWindow().setAttributes(screenBrightness);
}
});
WMLP = getWindow().getAttributes();
System.out.println("Screen Brightness OFF: "
+ WMLP.screenBrightness);
}
To programmatically turn off the screen and turn on the screen you can use Device Policy Manager.
To turn on the screen you can use PowerManager WakeLock (Wakelock is deprecated but currently working fine).
Use DevicePolicyManager.locknow(); to lock the screen. For that you need to register DevicePolicyManager in you app. A tutorial can be found here.
and use the code below to unlock the device.
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();
Hi and thanks for your help.
I have the following situation.
I have a Lock Screen Widget, when the user taps it the App Widget perform some tasks (updates itself).
The point is that: if the phone is in sleep mode and the user taps the App Widget, the App Widget would update itself, but the App Widget itself is not visible.
Therefore I need to wake up the device when the user taps the Lock Screen Widget - and after it can go to sleep again :-) -
Therefore I could use:
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK|PowerManager.ACQUIRE_CAUSES_WAKEUP, "bbbb");
wl.acquire();
In the AppWidgetProvider.
The point is: how do I call "release()" so that the device can go back to sleep?
If I do:
PowerManager pm = (PowerManager) ctxt.getSystemService(Context.POWER_SERVICE);
WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK|PowerManager.ACQUIRE_CAUSES_WAKEUP, "bbbb");
wl.acquire();
wl.release();
in the AppWidgetProvider simply the device never wakes up.
Any suggestion more than welcome!
Thanks!!
Well, I used a Handler to call
wl.release()
after 60 seconds:
public class AppWidget extends AppWidgetProvider {
#Override
public void onUpdate(Context ctxt, AppWidgetManager mgr, int[] appWidgetIds) {
ComponentName thisWidget = new ComponentName(ctxt, AppWidget.class);
int[] allWidgetIds = mgr.getAppWidgetIds(thisWidget);
Intent i = new Intent(ctxt, UpdateService.class);
i.putExtra("widgetsids", allWidgetIds);
ctxt.startService(i);
PowerManager pm = (PowerManager) ctxt
.getSystemService(Context.POWER_SERVICE);
final WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK
| PowerManager.ACQUIRE_CAUSES_WAKEUP, "bbbb");
wl.acquire();
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
wl.release();
}
}, 60000);
}
I'm writing app which must show alert dialog. To wake up device I'm using:
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK |
PowerManager.ACQUIRE_CAUSES_WAKEUP, "AlertWindow");
wl.acquire(); // wake up the screen
//... show alert to user
Handler h = new Handler();
h.postDelayed(new Runnable() {
public void run() {
wl.release(); // release lock
}
}, TIME);
But the problem is when alert window is open, screen is not turn off automatically.
adb shell dumpsys power before alert show:
mLocks.size=0:
after show but not closed:
mLocks.size=1:
SCREEN_BRIGHT_WAKE_LOCK 'AlertWindow'ACQUIRE_CAUSES_WAKEUP activated (minState=3, uid=10070, pid=5183)
Is there a way to turn off the screen using a handler? After closing the alert dialog lock is turned off ...
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