For most android devices below 6.0, the permission will be automatically granted as you have declared SYSTEM_ALERT_WINDOW in your manifest. However for some (Xiaomi and others) devices, this will not be the case. In this case you need to manually add permission to this app. Also, for 6.0 and above, you can add this (or also grant permission manually) Which 5.0 device are you using?<--(EDIT) Ok, I have just seen in your signature that it is a Xiaomi
Code invoquing the lockscreen should only be made after Activity_Resume if the conditions are met.
If p.SdkVersion>=23 Then
jo.RunMethod("requestSpecialPermissionIfNeeded",Null)
End If
and on Activity_Resume
If p.SdkVersion>=23 Then
Dim gotPermission As Boolean = J.RunMethod("getSpecialPermissionValue",Null)
if not(gotPermission) then Activity.Finish
End If
'proceed with locking. and make sure it is only made once.
and add this inline Java code
#if JAVA
import android.content.Intent;
import android.provider.Settings;
import android.net.Uri;
public static int OVERLAY_PERMISSION_REQ_CODE = 1234;
public void requestSpecialPermissionIfNeeded() {
if (!Settings.canDrawOverlays(this)) {
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, OVERLAY_PERMISSION_REQ_CODE);
}
}
public boolean getSpecialPermissionValue(){
return(Settings.canDrawOverlays(this));
}