How to Display Activity When the Screen Is Locked

Start Activity screen even if screen is locked in Android

You need the following permission in AndroidManifest.xml file:

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

Check the manifest details here. You can check this link on you query.

Activity starts but isn't visible when the screen is off or locked

The double-lifecycle event referenced here is a known limitation of the "window flags" approach to dismissing the keyguard, and wasn't fixed until API 27. What this means is that the Activity can pause & stop before it ever shows up on the screen.

If you don't finish() the Activity, it'll usually onResume() a second time, and everything will be fine. But if you do call finish() in onPause() or onStop(), that's going to cause the Activity to proceed to the destroyed state, and you'll never see it.

The solution is to remove any finish() calls that take place before the Activity becomes visible.

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

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.



Related Topics



Leave a reply



Submit