Android Activity Not Showing When Screen is Woken Up and Lock Screen Not Disabling
Asked Answered
A

2

7

I have a BroadcastReceiver that starts an Activity. If the Activity is started while the screen is on, it displays and everything is fine. However, on ICS and JB devices (I haven't tested GB or HC but the issue doesn't exist with Froyo) if the Activity is started while the screen is off, the lockscreen is not disabled, and the activity is not shown when the phone is unlocked (either through unlocking it manually or with the code I put in for post Froyo devices).

Why, on at least ICS and JB devices, does the lockscreen not get disabled without the mentioned code below, and why doesn't the activity show if the screen was off when the Activity was started?

Here's the code:

In the BroadcastReceiver:

Intent alarmAlert = new Intent(context, AlarmGoneOffActivity.class);
alarmAlert.putExtra(MyAlarmManager.ALARM_NUM_ID, alarm.ID);
alarmAlert.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | 
                    Intent.FLAG_ACTIVITY_NO_USER_ACTION);
context.startActivity(alarmAlert);

In AlarmGoneOffActivity.onCreate():

setContentView(R.layout.alarm_gone_off);

final Window win = getWindow();
win.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN | 
             WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | 
             WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | 
             WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON,
             WindowManager.LayoutParams.FLAG_FULLSCREEN | 
             WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | 
             WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | 
             WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
}

//so far all of my post froyo devices (ICS and JB no more GB)
// don't bypass the lockscreen unless we use this
if(Build.VERSION.SDK_INT > Build.VERSION_CODES.FROYO) {
    KeyguardManager  myKeyGuard = 
                    (KeyguardManager)getSystemService(Context.KEYGUARD_SERVICE);
    KeyguardLock myLock = myKeyGuard.newKeyguardLock("ShabbosAlarm");
    myLock.disableKeyguard();
}

Edit: I would really prefer not to use KeyguardLock.disableKeyguard() because it causes the keyguard to become disabled until KeyguardLock.reenableKeyguard() which is inconvenient. Any solutions?

Edit2: I can now confirm that the issue only exists on ICS and above. Was something changed that prevents the keyguard from being disabled? And even if there was, why is my Activity not showing when the screen is manually unlocked?

Appliance answered 19/10, 2012 at 17:49 Comment(5)
are there any device policy managers installed? check the doc. If DPM is installed, it does not workPinwork
I think you misunderstand. Using KeyguardLock.disableKeyguard() works fine. It is the workaround for the problem.Appliance
@Appliance Did you ever solve this? I'm facing the exact same problem and no matter how much I mess with the window flags, the keyguard does not disable...Excrescency
@Excrescency unfortunately I have not solved this issue yet, although I haven't really focused on that app in a while. I'll update if and when I find the solution.Appliance
@Appliance - Thanks. After working through the night on it, I managed to solve it without the use of the Keyguard permission. I'll post my answer later todayExcrescency
E
3

Here is the base implementation I used to achieve this. It's working for my users from 2.3-4.3. You can build on it from here I'm sure. I created this very short-lived Activity, just to handle this event - You can use it as a helper Class if needs be:

import android.app.Activity;
import android.os.Bundle;
import android.view.WindowManager;

public class BlankActivity extends Activity {

    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getWindow().setFlags(
                WindowManager.LayoutParams.FLAG_FULLSCREEN | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
                        | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON,
                WindowManager.LayoutParams.FLAG_FULLSCREEN | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
                        | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);


        // Your code here - Or if it doesn't trigger, see below
    }

    @Override
    public void onAttachedToWindow() {

        // You may need to execute your code here

    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {

        } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {

        }
    }

    @Override
    public void onPause() {
        super.onPause();

        finish();
    }
}

And in the Manifest:

    <activity
        android:name="com.your.package.BlankActivity"
        android:clearTaskOnLaunch="true"
        android:configChanges="keyboardHidden|orientation|screenSize"
        android:excludeFromRecents="true"
        android:launchMode="singleInstance"
        android:noHistory="true"
        android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen" >
    </activity>

Contrary to other related posts, the Translucent theme did not cause an issue, neither was there a need for a layout to be added to the Activity.

Hope it works for you too.

Excrescency answered 22/8, 2013 at 16:49 Comment(4)
Also, could you further explain the need for this being a "short-lived Activity"?Orometer
@ForrestBice This did work for me. I think I might have had to alter it slightly to get it to work with what I had going on, but it should work as is.Appliance
I didn't write the part about it being a short-lived activity, but I assume he meant he wrote an Activity as an example that finishs in onPause.Appliance
@Appliance thanks for the response. How significant are the options such as "android:clearTaskOnLaunch", etc? Also are any permissions required in the manifest for "FLAG_SHOW_WHEN_LOCKED", etc? I'm trying this now. Thanks for the help!Orometer
L
0

This code work for me. I put this in OnCreate activity

private fun wakeScreen() {
    val window = getWindow();
    window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON or
            WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD or
            WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED or
            WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON)
}

And on the activity manifest I put this

<activity android:name=".Alarm.AlarmNotifActivity"
        android:showOnLockScreen="true"
        android:showWhenLocked="true"
        android:launchMode="singleInstance"/>

Hope it will helps.

Locklear answered 1/3, 2020 at 15:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.