Android Code Snippet Draw on top of other apps permission

Starting from Android 6 a special permission is required if you want to use the SYSTEM_ALERT_WINDOW permission. This permission is not related to the runtime permissions.

You can put this code in a class to check whether your app has permission and open the settings page if needed:
B4X:
Sub Class_Globals
   Private ion As Object
   Private phone As Phone
End Sub

Public Sub Initialize
End Sub

Public Sub GetPermission As ResumableSub
   If phone.SdkVersion >= 23 Then
       Dim settings As JavaObject
       settings.InitializeStatic("android.provider.Settings")
       Dim ctxt As JavaObject
       ctxt.InitializeContext
       If settings.RunMethod("canDrawOverlays", Array(ctxt)) = True Then
           Return True
       End If
       Dim i As Intent
       i.Initialize("android.settings.action.MANAGE_OVERLAY_PERMISSION", "package:" & Application.PackageName)
       StartActivityForResult(i)
       Wait For ion_Event (MethodName As String, Args() As Object)
       Return settings.RunMethod("canDrawOverlays", Array(ctxt))
   Else
       Return True
   End If
End Sub

Private Sub StartActivityForResult(i As Intent)
   Dim jo As JavaObject = GetBA
   ion = jo.CreateEvent("anywheresoftware.b4a.IOnActivityResult", "ion", Null)
   jo.RunMethod("startActivityForResult", Array As Object(ion, i))
End Sub

Private Sub GetBA As Object
   Dim jo As JavaObject = Me
   Return jo.RunMethod("getBA", Null)
End Sub

It is an example of how to use StartActivityForResult in a class.
Note that in this case there isn't any result. We just use it to know when the user clicked on the back button.

Usage example:
B4X:
Sub Button1_Click
   Dim c As RequestDrawOverPermission 'this is the name of the class
   c.Initialize
   Wait For (c.GetPermission) Complete (Success As Boolean)
   Log("Permission: " & Success)
End Sub

And you should add the permission to the manifest editor:
B4X:
AddPermission(android.permission.SYSTEM_ALERT_WINDOW)

Depends on: JavaObject and Phone libraries.
 
Last edited:

Shivito1

Active Member
Licensed User
Is there a library needed? I am trying to use special permission however it is asking for missing library.
B4X:
Dim Permission As SpecialPermission
 

DonManfred

Expert
Licensed User
Longtime User

Star-Dust

Expert
Licensed User
Longtime User
SpecialPermission class is in my library, probably use SD_FloatingButton library.

If it is not necessary to use the code posted by Erel because it is already included in the library, see the examples
 
Last edited:

Shivito1

Active Member
Licensed User
SpecialPermission class is in my library, probably use SD_FloatingButton library.

If it is not necessary to use the code posted by Erel because it is already included in the library, see the examples
Thank you I updated the lib. However no i am getting this error:
B4X:
B4A version: 6.80
Parsing code.    Error
Error parsing program.
Error description: Missing Keyword: next
Occurred on line: 89 (Main)
End Sub
Also I see that (undeclared variable 'wait')
B4X:
    Wait For (Permission.ActivatePermissionResumable) Complete (Success As Boolean)
    If Success Then
Again Thanks for your help. :)
 

Star-Dust

Expert
Licensed User
Longtime User
It is possible to use the methods that Erel described in the first post also with Android 6.80, but you have to modify them.

My floatingButton class was born with B4A 6.50. so you could do it.

However, see also the indications of @JordiCP, because from the SDK 26 also the flags change to view fluctuating views.
 

Star-Dust

Expert
Licensed User
Longtime User
Okay it would appear my version of B4A is the issue.
Try this:

Class
B4X:
Sub Class_Globals
    Private ion As Object
End Sub

'Inizializza l'oggetto. Puoi aggiungere parametri a questo metodo,se necessario.
Public Sub Initialize

End Sub

public Sub Sdk_Version As Int
    Dim I As Int
    Dim J As JavaObject
    j.InitializeStatic("android.os.Build.VERSION")
    I=j.GetField("SDK_INT")
    Return I
End Sub

Public Sub GetPermission As Boolean
    If Sdk_Version >= 23 Then
        Dim settings As JavaObject
        settings.InitializeStatic("android.provider.Settings")
        Dim ctxt As JavaObject
        ctxt.InitializeContext
        Return settings.RunMethod("canDrawOverlays", Array(ctxt))
    Else
        Return True
    End If
End Sub

Public Sub OpenSettingPermission
    If Sdk_Version >= 23 Then
        Dim settings As JavaObject
        settings.InitializeStatic("android.provider.Settings")
        Dim ctxt As JavaObject
        ctxt.InitializeContext
        'If settings.RunMethod("canDrawOverlays", Array(ctxt)) = True Then Return
        Dim i As Intent
        i.Initialize("android.settings.action.MANAGE_OVERLAY_PERMISSION", "package:" & Application.PackageName)
        StartActivityForResult(i)
    End If
End Sub

Private Sub StartActivityForResult(i As Intent)
    Dim jo As JavaObject = GetBA
    ion = jo.CreateEvent("anywheresoftware.b4a.IOnActivityResult", "ion", Null)
    jo.RunMethod("startActivityForResult", Array As Object(ion, i))
End Sub

Private Sub GetBA As Object
    Dim jo As JavaObject = Me
    Return jo.RunMethod("getBA", Null)
End Sub

Code in the App
B4X:
Dim Permission As SpecialPermission
    Permission.Initialize
    ToastMessageShow("Setting is:" & Permission.GetPermission,True)
    If Permission.GetPermission=False Then Permission.OpenSettingPermission
 

Shivito1

Active Member
Licensed User
Code in the App
B4X:
Dim Permission As SpecialPermission
    Permission.Initialize
    ToastMessageShow("Setting is:" & Permission.GetPermission,True)
    If Permission.GetPermission=False Then Permission.OpenSettingPermission
[/QUOTE]
Thanks I will test it when I get of work XD
 

Tempomaster

Active Member
Licensed User
Longtime User
I used the code from post #1. He works fine except for one exception. Android 8.0 has a problem with my app. For "android:targetSdkVersion = '26' ", opening the overlay window will result in an error. The change to "android:targetSdkVersion = '23' " prevents the occurrence of this error. I was informed by hints from users of my app. This behavior has been confirmed by a test in the Android 8.0 emulator (Android SDK).

P.S. The setup window for enabling this permission works correctly.

Best regards,
Gunnar
 
Last edited:

Tempomaster

Active Member
Licensed User
Longtime User
Thank you for this hint. This knowledge is priceless. I just hope that it does not always go on in the Android development. That's similar to MS Windows.

Best regards,
Gunnar
 

Tempomaster

Active Member
Licensed User
Longtime User
Hello Star-Dust,

I have added the following lines:

B4X:
#If Java

    import android.os.Build;

    private void sharedConstructing(Context context) {
        super.setClickable(true);

        this.context = context;
        windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);

        if (Build.VERSION.SDK_INT >= 26) {
           params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,//TYPE_SYSTEM_ALERT,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
                PixelFormat.TRANSLUCENT);
        } else {
           params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_PHONE,//TYPE_SYSTEM_ALERT,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
                PixelFormat.TRANSLUCENT);
        }

        params.gravity = Gravity.TOP | Gravity.LEFT;
        params.x = 0;
        params.y = 100;

        windowManager.addView(this, params);
                }
#End If

The manifest contains the following line:

B4X:
AddManifestText(<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>)

I get an error message when compiling (see attachment). Something is missing. I do not know what leather. I apologize for my ignorance. About Help I would be happy.

error.png


Best regards,
Gunnar
 
Top