After releasing a WakeLock screen is not turned off - android

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 ...

Related

To detect the screenlock even when the app is sleeping

I have an android application that needs to be locked (redirect to the login page) whenever the user presses the lock button. The user can lock the phone while on my application or while on other applications/home screen. In both of these scenarios, I need my application to be locked.
This is working fine in case the phone is locked from an application :
#Override
protected void onStop() {
super.onStop();
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
boolean isScreenOn = pm.isInteractive();
if (!isScreenOn) {
LogUtils.logD(TAG, "Screen is off, Locking the application");
// Lock the application code
}
}
But I am not able to figure out the second scenario when the user has moved away from the application and then locks it. I do not want to start a service or any background thread for this purpose.
Try to add else and return at the end of the code so the activity will still awake in background process
#Override
protected void onStop() {
super.onStop();
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
boolean isScreenOn = pm.isInteractive();
if (!isScreenOn) {
LogUtils.logD(TAG, "Screen is off, Locking the application");
// Lock the application code
} else{
return true;
}

Activity does not start until the screen is turned on

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

Android whatsapp like call notification

Am developing a voip application. There is a background service which shows incoming call notifications, which works as expected (showing an incoming call dialog) when the phone is not locked and app is in background state.
How can i generate a dialog with interactive buttons, like whatsapp incoming call notification; even when the phone is locked?
Any heads up on this one or documentation that i can look up?
I can send an inapp notification for an incoming call, but that seems to be not enough for the purpose. I would need a full blow dialog interface which has a button or similar that would in turn open the application.
I am using Quickblox as the voip service provider.
Thanks in advance
I have tried unlocking the phone, on a button press
Here is my code to open a dialog from the background service.
viewToShowOnLockedScreen = View.inflate(getApplicationContext(), R.layout.activity_background_call, null);
viewToShowOnLockedScreen.setTag(TAG);
int top = getApplicationContext().getResources().getDisplayMetrics().heightPixels / 2;
final WindowManager.LayoutParams mLayoutParams = new WindowManager.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, Utils.dpToPx(300), 0, 0,
WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON ,
PixelFormat.RGBA_8888);
viewToShowOnLockedScreen.setVisibility(View.VISIBLE);
Animation mAnimation = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.abc_slide_in_top);
viewToShowOnLockedScreen.startAnimation(mAnimation);
mWindowManager.addView(viewToShowOnLockedScreen, mLayoutParams);
mWindowManager.updateViewLayout(viewToShowOnLockedScreen, mLayoutParams);
And here is the code to unlock the device on a button press. Though this looks like it unlocks the phone, the screen is on, but the phone is still locked. Home button does'nt work.
KeyguardManager km = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
final KeyguardManager.KeyguardLock kl = km .newKeyguardLock("MyKeyguardLock");
kl.disableKeyguard();
PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
PowerManager.WakeLock wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK
| PowerManager.ACQUIRE_CAUSES_WAKEUP
| PowerManager.ON_AFTER_RELEASE, "MyWakeLock");
wakeLock.acquire();
Not really an answer, a work around i should say.
I coul'dnt unlock the screen. However I have updated the application to listen to Intent.ACTION_USER_PRESENT and added necessary to logic in the app to answer calls once the user has unlocked the device at the point of incoming call. Here is the code.
mUnlockStatusFilter = new IntentFilter();
mUnlockStatusFilter.addAction(Intent.ACTION_USER_PRESENT);
mUnlockStateIntentReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent i) {
if (i.getAction().equals(Intent.ACTION_USER_PRESENT)) {
//Do something phone is unlocked
Log.d(TAG,"Screen unlocked");
if(isWaitingForUnlock){
stopCallNotification();
if(Foreground.get().isForeground()){
Log.d(TAG, "App is in foreground; Sending a broadcast");
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
intent.setAction(BaseActivityWithSignalling.onReceiveNewSession);
intent.putExtra("code", BaseActivityWithSignalling.onReceiveNewSessionCode);
sendBroadcast(intent);
Log.d(TAG, "Send broadcast for onReceiveNewSession");
}else{
Log.d(TAG, "App is in background; Showing activity");
Intent showCallingFromBG = new Intent(LMService.this, BackgroundCall.class);
showCallingFromBG.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(showCallingFromBG);
}
}
}
}
};
registerReceiver(mUnlockStateIntentReceiver, mUnlockStatusFilter);

Screen is not sleeping when touching the screen

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

how to show popup Activity or Dialog when phone is locked?

I am trying to show a activity or a dialog when the phone is locked.
I have tried using a WakeLock but it did not work and I can only see the activity once my phone is unlocked?
What is the proper way to do this?
To show activity without dismissing the keyguard try this:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView();
You should use the KeyGuardManager to unlock the device automatically and then acquire your Wake Lock.
KeyguardManager kgm = (KeyguardManager)getSystemService(Context.KEYGUARD_SERVICE);
boolean isKeyguardUp = kgm.inKeyguardRestrictedInputMode();
KeyguardLock kgl = kgm.newKeyguardLock("Your Activity/Service name");
if(isKeyguardUp){
kgl.disableKeyguard();
isKeyguardUp = false;
}
wl.acquire(); //use your wake lock once keyguard is down.
To show a popup on top of a lock screen try this, from my other answer:
AlertDialog alertDialog = new AlertDialog.Builder(context).create();
alertDialog.getWindow().setType(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
alertDialog.show();
To show activity on top of a lock screen, or basically remove the lock screen when activity is starts, try this:
public void onCreate(Bundle savedInstanceState){
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
...
}
Both of those options require api 5+

Categories

Resources