简体   繁体   中英

Waking up the Device from a Lockscreen and showing an Activity

I'm coding an alarm application. When it's time to ring, I want the device to wake up and show my activity that allows the user to disable the alarm. It's working fine except for when the device is locked (aka on the lockscreen).

I've tried many of the answers I've found about this, but almost everything seems to be deprecated and I obviously want to avoid using these methods. With the current code I have (where I tried to combine the answers I've found) it will vibrate for a very short time (which my alarm is supposed to do when it rings, but normally it would continue until the alarm is disabled), but not more. When unlocking the device afterwards, the activity to disable the alarm is not shown and the alarm is not ringing (which is done by that activity). However I have checked and made sure that the onCreate() method is executed completely and without any errors until the end.

In my BroadcastReceiver (I'm using AlarmManager to execute my code at the necessary time):


    @Override
    public void onReceive(Context context, Intent intent)
    {
        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "myalarmapp:alarm.");
        wl.acquire(600000);

        Intent startAlarmActivity = new Intent(context, AlarmActivity.class);

        context.startActivity(startAlarmActivity);

        wl.release();
    }

In my activity's onCreate() method:

        setContentView(R.layout.activity_alarm);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {

            setShowWhenLocked(true);
            setTurnScreenOn(true);
            KeyguardManager keyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
            keyguardManager.requestDismissKeyguard(this, null);
        }
        else {

            getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
                    WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                    WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON |
                    WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        }

In my AndroidManifest.xml:

    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.DISABLE_KEYGUARD" />

...

        <activity
            android:name=".view.AlarmActivity"
            android:excludeFromRecents="true"
            android:showOnLockScreen="true"
            android:showWhenLocked="true"
            android:turnScreenOn="true"
            android:showForAllUsers="true">
        </activity>

Preferably I would want the activity to show "above" the lockscreen, but that is not my main priority. It would be enough if the activity starts and the user has to unlock their phone to see it, all while the activity is vibrating and ringing the phone. Note: I'm testing my application on Android 7.1 and my current minimum API level is 21.

I've figured it out, it actually wasn't something wrong with the wakelocks. The problem was that I also canceled the alarm (and called finish() ) in the onStop() method of the activity. When the screen is locked onStop() was called right away, so it also closed the activity and stopped ringing.

I fixed it by checking the following booleans (credit: Detecting when screen is locked ):


        KeyguardManager keyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
        boolean isPhoneLocked = keyguardManager.inKeyguardRestrictedInputMode();

        PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
        boolean isScreenAwake = powerManager.isInteractive();

With that solution I also managed to show it on the lockscreen, without needing the options set for the activity in the AndroidManifest.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM