Android Code Snippet Draw on top of other apps permission

Discussion in 'Code Snippets' started by Erel, Mar 11, 2018.

  1. Erel

    Erel Administrator Staff Member Licensed User

    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:
    Code:
    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 startForActivity 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:
    Code:
    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:
    Code:
    AddPermission(android.permission.SYSTEM_ALERT_WINDOW)
    Depends on: JavaObject and Phone libraries.
     
    Shivito1, Jamie8763, JordiCP and 5 others like this.
  2. Shivito1

    Shivito1 Active Member Licensed User

    Is there a library needed? I am trying to use special permission however it is asking for missing library.
    Code:
    Dim Permission As SpecialPermission
     
  3. DonManfred

    DonManfred Expert Licensed User

    where did you get this code from?

    In the example above i just find
     
    Shivito1 likes this.
  4. Star-Dust

    Star-Dust Expert 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
     
    Last edited: Mar 14, 2018
    Shivito1 likes this.
  5. Erel

    Erel Administrator Staff Member Licensed User

    The libraries needed for the code in the first post are JavaObject and Phone.
     
    Shivito1 likes this.
  6. Shivito1

    Shivito1 Active Member Licensed User

    Thank you I updated the lib. However no i am getting this error:
    Code:
    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')
    Code:
    Wait For (Permission.ActivatePermissionResumable) Complete (Success As Boolean)
        
    If Success Then
    Again Thanks for your help. :)
     
  7. Shivito1

    Shivito1 Active Member Licensed User

    Okay it would appear my version of B4A is the issue.
     
  8. DonManfred

    DonManfred Expert Licensed User

    yes
     
    Shivito1 likes this.
  9. Star-Dust

    Star-Dust Expert Licensed 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.
     
    Shivito1 likes this.
  10. Star-Dust

    Star-Dust Expert Licensed User

    Try this:

    Class
    Code:
    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
    Code:
    Dim Permission As SpecialPermission
        Permission.Initialize
        
    ToastMessageShow("Setting is:" & Permission.GetPermission,True)
        
    If Permission.GetPermission=False Then Permission.OpenSettingPermission
     
    Shivito1 likes this.
  11. Shivito1

    Shivito1 Active Member Licensed User

    Code in the App
    Code:
    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
     
  12. Gunnar Daehling

    Gunnar Daehling Member Licensed 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: Apr 6, 2018
  13. Star-Dust

    Star-Dust Expert Licensed User

  14. Gunnar Daehling

    Gunnar Daehling Member Licensed 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
     
  15. Gunnar Daehling

    Gunnar Daehling Member Licensed User

    Hello Star-Dust,

    I have added the following lines:

    Code:
    #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:

    Code:
    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
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice