2

I am facing the issue mentioned here, Screen Blackout. Am not able find solution for it, Let me know if there is a way to fix it.

Theja
  • 744
  • 1
  • 7
  • 24

2 Answers2

0

Today I faced also this problem and I found a kida hacky solution.

The key is to re-enable keyguard, dismiss it and again re-enable it.

private KeyguardManager.KeyguardLock mLock;
public static final String KEYLOCK_NAME = "key_lock";

@Override
protected void onPause() {
    super.onPause();
    // Call it anywhere you need, onPause is just an example
    if (enableKeyguard()) {
        disableKeyguard();
        enableKeyguard();
    }
}
/**
 * Dismisses lockscreen
 */
public void disableKeyguard() {
    if (mLock == null) {
        KeyguardManager manager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
        mLock = manager.newKeyguardLock(KEYLOCK_NAME);
        mLock.disableKeyguard();
    }
}

/**
 * Re-enables lockscreen
 * @return true if lockscreen was previously disabled and now it is enabled again, otherwise false
 */
public boolean enableKeyguard() {
    if (mLock != null) {
        mLock.reenableKeyguard();
        mLock = null;
        return true;
    }

    return false;
}
mroczis
  • 1,879
  • 2
  • 16
  • 18
0

You can use this code:

private BroadcastReceiver mScreenOffReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        SimpleLog.i(TAG, intent.getAction());
        if(intent.getAction().equals(Intent.ACTION_SCREEN_OFF)
                || intent.getAction().equals(Intent.ACTION_SCREEN_ON)){

            keyguardManager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
            keyguardLock = keyguardManager.newKeyguardLock("");
            keyguardLock.disableKeyguard();

            startActivity(lockIntent);

            // **This line is important!!!**
            keyguardLock.reenableKeyguard(); 
        }
    }
};
dpk
  • 21
  • 3