List of all apps installed by the user?

Kiffi

Well-Known Member
Licensed User
Longtime User
Hello,

with the following snippet i am able to get a list of all packages on my phone:

B4X:
Dim pm As PackageManager
Dim packages As List
packages = pm.GetInstalledPackages
For i = 0 To packages.Size - 1
    Log(packages.Get(i))
Next

But how to get a list of all packages installed by the user? (all Apps i am able to uninstall)

Thanks & Greetings ... Kiffi
 

Kiffi

Well-Known Member
Licensed User
Longtime User
in the meantime i found the following java snippet to figure out if an app is a system app (means: contains in the OS image) or not:

B4X:
for (int i = 0; i < packs.size(); i++) {
  PackageInfo p = packs.get(i);
  if ((p.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
    // System-App!
  }
[...]

(Link: Anwendungen auflisten OHNE Systemapps - Problem - Android Forum - AndroidPIT (in german language))

but with B4A i am not able to get the PackageInfo-Object. Any hints?

TIA & Greetings ... Kiffi
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
Try to use the reflection lib...I personally know nothing of reflection, but seems to be magical accessing "offworld"!
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
B4X:
   Dim Obj1, Obj2, Obj3 As Reflector
   Dim size, i, flags As Int
   Dim msg, name As String
   Obj1.Target = Obj1.GetContext
   Obj1.Target = Obj1.RunMethod("getPackageManager") ' PackageManager
   Obj1.Target = Obj1.RunMethod2("getInstalledPackages", 0, "java.lang.int") ' List<PackageInfo>
   size = Obj1.RunMethod("size")
   For i = 0 To size -1
      Obj2.Target = Obj1.RunMethod2("get", i, "java.lang.int") ' PackageInfo
      name = Obj2.GetField("packageName")
      Obj3.Target = Obj2.GetField("applicationInfo") ' ApplicationInfo      
      flags = Obj3.GetField("flags")      
      msg = msg & name & " : " & flags & CRLF
   Next   
   Msgbox(msg,"")
 
Upvote 0

ZJP

Active Member
Licensed User
Longtime User
 
Last edited:
Upvote 0

rtesluk

Member
Licensed User
Longtime User
Handy Piece Of Code

July 26 2011
04:00 Hours

Thanks for this snippet of code.

I created a little app around it.

Ray Tesluk :sign0098:
 

Attachments

  • ReflectPkg.zip
    14.1 KB · Views: 673
Upvote 0

fabero

Member
Licensed User
Longtime User
Sorry but I don't understand how I can list only user installed apps with flags..

:BangHead:
 
Upvote 0

fabero

Member
Licensed User
Longtime User
Oh great.. But I don't understand this approach..

B4X:
Dim Obj1, Obj2, Obj3 As Reflector
   Dim size, i, flags As Int
   Dim msg, name As String
   Obj1.Target = Obj1.GetContext
   Obj1.Target = Obj1.RunMethod("getPackageManager") ' PackageManager
   Obj1.Target = Obj1.RunMethod2("getInstalledPackages", 0, "java.lang.int") ' List<PackageInfo>
   size = Obj1.RunMethod("size")
   For i = 0 To size -1
      Obj2.Target = Obj1.RunMethod2("get", i, "java.lang.int") ' PackageInfo
      name = Obj2.GetField("packageName")
      Obj3.Target = Obj2.GetField("applicationInfo") ' ApplicationInfo      
      flags = Obj3.GetField("flags")   
               If Bit.And(flags, 1)  = 0 Then
                   'app is not in the system image
                msg = msg & name & " : " & flags & CRLF
               End If      
      
   Next   
   Msgbox(msg,"")

This slice of code "query" non system app with that declaration?
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
Seems to work fine for me. I added getting the "proper" name. Sorry for the messy layout, I've got to go now.
B4X:
   Dim args(1) As Object
   Dim Obj1, Obj2, Obj3, Obj4 As Reflector
   Dim size, i, flags, count As Int
   Dim Types(1), msg, name As String
   Obj1.Target = Obj1.GetContext
   Obj1.Target = Obj1.RunMethod("getPackageManager") ' PackageManager
   Obj2.Target = Obj1.RunMethod2("getInstalledPackages", 0, "java.lang.int") ' List<PackageInfo>
   size = Obj2.RunMethod("size")
   For i = 0 To size -1
      Obj3.Target = Obj2.RunMethod2("get", i, "java.lang.int") ' PackageInfo
      name = Obj3.GetField("packageName")
      Obj3.Target = Obj3.GetField("applicationInfo") ' ApplicationInfo      
      flags = Obj3.GetField("flags")   
   If Bit.And(flags, 1)  = 0 Then
       'app is not in the system image
          
      args(0) = Obj3.Target
      types(0) = "android.content.pm.ApplicationInfo" 
      name = Obj1.RunMethod4("getApplicationLabel", args, types)
    msg = msg & name & " : " & flags & CRLF
      count = count + 1
   End If      
      
   Next   
   Msgbox(msg,count) 
   Return
 
Upvote 0

JakeBullet70

Well-Known Member
Licensed User
Longtime User
I have been trying for an hour to add to code to get the icon. I found 'getApplicationIcon ' in the Android documentation but just cannot wrap my head around how to add this. BANG-HEAD!!

Any help would be appreciated.


Seems to work fine for me. I added getting the "proper" name. Sorry for the messy layout, I've got to go now.
B4X:
   Dim args(1) As Object
   Dim Obj1, Obj2, Obj3, Obj4 As Reflector
   Dim size, i, flags, count As Int
   Dim Types(1), msg, name As String
   Obj1.Target = Obj1.GetContext
   Obj1.Target = Obj1.RunMethod("getPackageManager") ' PackageManager
   Obj2.Target = Obj1.RunMethod2("getInstalledPackages", 0, "java.lang.int") ' List<PackageInfo>
   size = Obj2.RunMethod("size")
   For i = 0 To size -1
      Obj3.Target = Obj2.RunMethod2("get", i, "java.lang.int") ' PackageInfo
      name = Obj3.GetField("packageName")
      Obj3.Target = Obj3.GetField("applicationInfo") ' ApplicationInfo     
      flags = Obj3.GetField("flags")  
   If Bit.And(flags, 1)  = 0 Then
       'app is not in the system image
         
      args(0) = Obj3.Target
      types(0) = "android.content.pm.ApplicationInfo"
      name = Obj1.RunMethod4("getApplicationLabel", args, types)
    msg = msg & name & " : " & flags & CRLF
      count = count + 1
   End If     
     
   Next  
   Msgbox(msg,count)
   Return
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
I have been trying for an hour to add to code to get the icon. I found 'getApplicationIcon ' in the Android documentation but just cannot wrap my head around how to add this. BANG-HEAD!!

First: LOL... Tried the forumsearch? :D

You dont need reflector for this. Just add Phone library
B4X:
    Activity.LoadLayout("Layout1")
    lv.Clear
    Dim pm As PackageManager
    Dim packages As List
    packages = pm.GetInstalledPackages
    For i = 0 To packages.Size - 1
    Dim p As String = packages.Get(i)
        Dim bdw As BitmapDrawable = pm.GetApplicationIcon(p)
        lv.AddTwoLinesAndBitmap(pm.GetApplicationLabel(p),packages.Get(i),bdw.Bitmap)
        Log(packages.Get(i))
    Next

packagemanager001.png
 
Last edited:
Upvote 0

JakeBullet70

Well-Known Member
Licensed User
Longtime User
OK. to also get the icon from agraham code above add these 2 lines

B4X:
    Dim icon As BitmapDrawable '  *** NEW LINE  ***
    If Bit.AND(flags, 1)  = 0 Then
           'app is not in the system image
          args(0) = Obj3.Target
            Types(0) = "android.content.pm.ApplicationInfo" 
            name = Obj1.RunMethod4("getApplicationLabel", args, Types)
            icon = Obj1.RunMethod4("getApplicationIcon", args, Types)  '  *** NEW LINE  ***
            msg = msg & name & " : " & flags & CRLF
            count = count + 1           
          End If
 
Upvote 0
Top