Android Question Is it possible to check whether another application has a certain permission under Android?

Filippo

Expert
Licensed User
Longtime User
Hi,

as the title says, i want to check if an installed app has a certain permission.

I found this code in stackoverflow, but I don't know how to implement it in B4a.

Can someone help me?
 
Solution
add this to your manifest:
B4X:
AddPermission(android.permission.QUERY_ALL_PACKAGES)

and plug this code in:
B4X:
    Dim packagemanager As JavaObject
    Dim packageinfo As JavaObject
    Dim jo As JavaObject
    jo.InitializeContext
 
    packagemanager = jo.RunMethod("getPackageManager",Null)
    Dim packages As List = packagemanager.RunMethod("getInstalledPackages",Array(4096))    ' constant for permissions
 
    Log(packages.Size & " packages")
    For Each packageinfo As JavaObject In packages
        Dim permissions() As String = packageinfo.GetField("requestedPermissions")
        If permissions = Null Then
            Log("no perms for " & packageinfo.GetField("packageName"))
        Else
            Log("perms for " &...

drgottjr

Expert
Licensed User
Longtime User
add this to your manifest:
B4X:
AddPermission(android.permission.QUERY_ALL_PACKAGES)

and plug this code in:
B4X:
    Dim packagemanager As JavaObject
    Dim packageinfo As JavaObject
    Dim jo As JavaObject
    jo.InitializeContext
 
    packagemanager = jo.RunMethod("getPackageManager",Null)
    Dim packages As List = packagemanager.RunMethod("getInstalledPackages",Array(4096))    ' constant for permissions
 
    Log(packages.Size & " packages")
    For Each packageinfo As JavaObject In packages
        Dim permissions() As String = packageinfo.GetField("requestedPermissions")
        If permissions = Null Then
            Log("no perms for " & packageinfo.GetField("packageName"))
        Else
            Log("perms for " & packageinfo.GetField("packageName") & ":")
            Dim i As Int
            For i = 0 To permissions.Length - 1
                Log( permissions(i) )     ' <--- you can choose to list only permissions matching some criterion of yours
            Next
        End If
        Log("------------------------------------------------------")
    Next


output to log will give you a list of all installed packages, their name and permissions. you can pick and choose

B4X:
perms for com.google.android.apps.restore:
android.permission.ACCESS_FINE_LOCATION
android.permission.ACCESS_NETWORK_STATE
android.permission.ACCESS_WIFI_STATE
android.permission.BACKUP
android.permission.CHANGE_WIFI_STATE
android.permission.FOREGROUND_SERVICE
android.permission.FOREGROUND_SERVICE_DATA_SYNC
android.permission.GET_PACKAGE_SIZE
android.permission.INTERNET
android.permission.MANAGE_EXTERNAL_STORAGE
android.permission.MANAGE_USB
android.permission.MANAGE_USERS
android.permission.MANAGE_WIFI_NETWORK_SELECTION
android.permission.NEARBY_WIFI_DEVICES
android.permission.POST_NOTIFICATIONS
android.permission.NFC
android.permission.QUERY_ALL_PACKAGES
android.permission.READ_CALL_LOG
android.permission.READ_CONTACTS
android.permission.READ_EXTERNAL_STORAGE
android.permission.READ_PRIVILEGED_PHONE_STATE
android.permission.RECEIVE_BOOT_COMPLETED


you can do some of this with the phone library (dim pm as packagemanger, pm.getinstalledpackages), but i think you'll have to use javaobject for the real work, so i just did the whole thing that way.
 
Last edited:
Upvote 1
Solution

Filippo

Expert
Licensed User
Longtime User
add this to your manifest:
B4X:
AddPermission(android.permission.QUERY_ALL_PACKAGES)

and plug this code in:
B4X:
    Dim packagemanager As JavaObject
    Dim packageinfo As JavaObject
    Dim jo As JavaObject
    jo.InitializeContext
 
    packagemanager = jo.RunMethod("getPackageManager",Null)
    Dim packages As List = packagemanager.RunMethod("getInstalledPackages",Array(4096))    ' constant for permissions
 
    Log(packages.Size & " packages")
    For Each packageinfo As JavaObject In packages
        Dim permissions() As String = packageinfo.GetField("requestedPermissions")
        If permissions = Null Then
            Log("no perms for " & packageinfo.GetField("packageName"))
        Else
            Log("perms for " & packageinfo.GetField("packageName") & ":")
            Dim i As Int
            For i = 0 To permissions.Length - 1
                Log( permissions(i) )     ' <--- you can choose to list only permissions matching some criterion of yours
            Next
        End If
        Log("------------------------------------------------------")
    Next


output to log will give you a list of all installed packages, their name and permissions. you can pick and choose

B4X:
perms for com.google.android.apps.restore:
android.permission.ACCESS_FINE_LOCATION
android.permission.ACCESS_NETWORK_STATE
android.permission.ACCESS_WIFI_STATE
android.permission.BACKUP
android.permission.CHANGE_WIFI_STATE
android.permission.FOREGROUND_SERVICE
android.permission.FOREGROUND_SERVICE_DATA_SYNC
android.permission.GET_PACKAGE_SIZE
android.permission.INTERNET
android.permission.MANAGE_EXTERNAL_STORAGE
android.permission.MANAGE_USB
android.permission.MANAGE_USERS
android.permission.MANAGE_WIFI_NETWORK_SELECTION
android.permission.NEARBY_WIFI_DEVICES
android.permission.POST_NOTIFICATIONS
android.permission.NFC
android.permission.QUERY_ALL_PACKAGES
android.permission.READ_CALL_LOG
android.permission.READ_CONTACTS
android.permission.READ_EXTERNAL_STORAGE
android.permission.READ_PRIVILEGED_PHONE_STATE
android.permission.RECEIVE_BOOT_COMPLETED


you can do some of this with the phone library (dim pm as packagemanger, pm.getinstalledpackages), but i think you'll have to use javaobject for the real work, so i just did the whole thing that way.
Thank you very much!
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
let me update this a little. previous version showed packages' permission requests. this version shows whether or not a particular permission was granted. check the log capture.

B4X:
    Dim packagemanager As JavaObject
    Dim packageinfo As JavaObject
    Dim jo As JavaObject
    jo.InitializeContext
  
    packagemanager = jo.RunMethod("getPackageManager",Null)
    Dim packages As List = packagemanager.RunMethod("getInstalledPackages",Array(4096))    ' constant for permissions
  
    Log(packages.Size & " packages")
    For Each packageinfo As JavaObject In packages
        Dim packageName As String = packageinfo.GetField("packageName")
        Dim permissions() As String = packageinfo.GetField("requestedPermissions")
        If permissions = Null Then
            Log("no perms for " & packageName)
        Else
            Log("perms for " & packageName & ":")
            Dim i As Int
            For i = 0 To permissions.Length - 1
                Dim permissionStatus As Int = packagemanager.RunMethod("checkPermission", Array As Object(permissions(i), packageName))
          
                If permissionStatus = 0 Then
                    Log(permissions(i) & " granted " & Chr(0x2705))
                else if permissionStatus = -1 Then
                    Log(permissions(i) & " not granted")
                Else
                    Log(permissions(i) & " Permission status unknown...")
                End If
            Next
        End If
        Log("------------------------------------------------------")
    Next

B4X:
android.permission.CAPTURE_SECURE_VIDEO_OUTPUT not granted
android.permission.ADD_ALWAYS_UNLOCKED_DISPLAY not granted
android.permission.CREATE_VIRTUAL_DEVICE not granted
android.permission.TOGGLE_AUTOMOTIVE_PROJECTION granted ✅
android.permission.REQUEST_COMPANION_PROFILE_AUTOMOTIVE_PROJECTION not granted
android.permission.ADD_TRUSTED_DISPLAY not granted
android.permission.ASSOCIATE_COMPANION_DEVICES not granted
android.permission.RECEIVE_SENSITIVE_NOTIFICATIONS not granted
------------------------------------------------------
perms for com.tubitv:
android.permission.INTERNET granted ✅
android.permission.ACCESS_NETWORK_STATE granted ✅
android.permission.ACCESS_WIFI_STATE granted ✅
android.permission.ACCESS_COARSE_LOCATION not granted
android.permission.FOREGROUND_SERVICE granted ✅
android.permission.WAKE_LOCK granted ✅
com.google.android.c2dm.permission.RECEIVE granted ✅
com.android.providers.tv.permission.READ_EPG_DATA not granted
com.android.providers.tv.permission.WRITE_EPG_DATA not granted
com.amazon.device.permission.COMRADE_CAPABILITIES not granted
android.permission.READ_EXTERNAL_STORAGE not granted
android.permission.RECEIVE_BOOT_COMPLETED granted ✅

i like the "com.amazon.device.permission.COMRADE_CAPABILITIES not granted" permission status. this was due to my having cancelled my communist party affiliation many years ago.
 
Last edited:
Upvote 0
Top