Getting the Manifest-Permissions of an App

DKCERT

Member
Licensed User
Longtime User
With reference to this thread I have run into a small problem.

I would like to retrive the permissions of a given app. I have tried to "convert" the code in this link (How to get manifest permissions of any installed Android app) into B4A code. But the Reflection Lib still play tricks on me (combined with limited skills and such... :) ).

Can someone post code that shows how to get a named app's permissions? Something like:

B4X:
Sub GetAppPermissions(sAppName As String) As List

End Sub

The code from the link that should do the trick looks like this:

B4X:
final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
final List pkgAppsList = getPackageManager().queryIntentActivities(mainIntent, 0);

for (Object obj : pkgAppsList) {
  ResolveInfo resolveInfo = (ResolveInfo) obj;
  PackageInfo packageInfo = null;
  try {
    packageInfo = getPackageManager().getPackageInfo(resolveInfo.activityInfo.packageName, PackageManager.GET_PERMISSIONS);
  } catch (NameNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
  }

  String[] requestedPermissions = packageInfo.requestedPermissions;
}

Thanks in advance.

Relevant Links:
Hot to get manifest permissions of any installed Android app - Stack Overflow
PackageInfo | Android Developers
Manifest.permission | Android Developers
 
Last edited:

DKCERT

Member
Licensed User
Longtime User
Start by reading through this this thread. Post #14 gets a PackageInfo for each app then the PackageName. You should be able to build on this.

I already have the list of installed Apps running where I get the package name, label and mask out the types of apps I want using the flag etc.

What I would like to get a given app's Manifest Permissions list.

I have tried to use the Reflection Lib to do that the past few days. As a start I modified your code which you refer to. But I don't have the overview yet.
That's why I seek help so I can see it used in different ways that corresponds to my small project (makes it easier to learn).
 
Last edited:
Upvote 0

DKCERT

Member
Licensed User
Longtime User
Ok, here is what I (think I) know:

- The wanted Permissions are located in the PackageInfo object (?)
- It's the PermissionInfo[] that holds the wanted permissions (?)
- Permissions are an Array of all <permission> tags under <manifest>, or null if none.
- The result is a list based on: Manifest.permission

My testcode is throwing exceptions or at best simply returning null (even when I know there are permissions). How do I get this info?
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can use this code (depends on the Reflection library):
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Dim p() As String
   p = GetPermissions("anywheresoftware.b4a.designer")
   For i = 0 To p.Length - 1
      Log(p(i))
   Next
End Sub

Sub GetPermissions(Package As String) As String()
   Dim r As Reflector
   r.Target = r.GetContext
   r.Target = r.RunMethod("getPackageManager")
   'get PacakgeInfo
   r.Target = r.RunMethod4("getPackageInfo", Array As Object(Package, 0x00001000), _
      Array As String("java.lang.String", "java.lang.int"))
   Dim permissions() As String
   permissions = r.GetField("requestedPermissions")
   If permissions = Null Then
      Dim permissions(0) As String
   End If
   Return permissions
End Sub
 
Upvote 0

DKCERT

Member
Licensed User
Longtime User
Erel, thank you so much --> :sign0188: :sign0188: :sign0188:

I can see why it gave me trouble. Too big a chunk for my current skill-level. But your code just kicked me in a new direction.
 
Upvote 0
Top