Android Question how to remove System alert window ?

peacemaker

Expert
Licensed User
Longtime User
v.RemoveView gives error
java.lang.RuntimeException: Object should first be initialized (View).

If to make v as Object, and later make it = Null - no error, and no help :(
 
Last edited:
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
No, the same error. Sample project attached.
 

Attachments

  • v.0.1.zip
    7.3 KB · Views: 179
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
Oh, indeed !
Now it's OK :)

If to add a TextView and Button to enter unlocking password - it would be finally useful code.

upd: but it locks the device under Android 4.2, partially locks under Android 4.4, but does not under Android 5.0
 

Attachments

  • 0.11.zip
    8.2 KB · Views: 182
Last edited:
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
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.
B4X:
  If p.SdkVersion>=23 Then
     jo.RunMethod("requestSpecialPermissionIfNeeded",Null)
   End If
and on Activity_Resume
B4X:
  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
B4X:
#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));
}
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
Thanks, but ... your code is for Android 6+. And if even 4.4 ... 5.0 are not locked well (notification area is pulled as usual) - no use to talk about 6+ :(
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
Yes, I see it is more complicated than expected. There are a lot of stackoverflow posts about this and it does not seem simple...sorry I can't help
 
Upvote 0
Top