Android Code Snippet Get List Of Installed Apps and their icons

Hi all.

This function logs a list of installed apps and can also get their icons. I have had this for a few years and it could be modernized to the new b4a features but it works so... Right now it just logs the output but there is also remarked out code to add everything to a ListView

This is not all my work but a little code from all over the b4a forums.

Have fun!

B4X:
Private Sub ShowInstalledApps 'ignore

       Dim args(1) As Object
       Dim Obj1, Obj2, Obj3 As Reflector
       Dim size, i, flags As Int
       Dim Types(1), name,packName As String
       Dim icon As BitmapDrawable
       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
          packName = Obj3.GetField("packageName")
 
          Obj3.Target = Obj3.GetField("applicationInfo") ' ApplicationInfo
          flags = Obj3.GetField("flags")
 
          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)
              icon = Obj1.RunMethod4("getApplicationIcon", args, Types)
              'lv.AddTwoLinesAndBitmap2(name,"",icon.Bitmap,packName)
              Log("-------------------------------------------------------------")
              Log(name & " --> " & packName)
              Log("-------------------------------------------------------------")
           End If
      Next

End Sub
 
Last edited:

Paul Edwards

Member
Licensed User
Longtime User
1. You should always start a new thread in the questionsforum for any question you have...
2. Use the packagemanager and search for the correct packagename.
for ex. to start you search

B4X:
    Dim pm As PackageManager  'phone library
   For Each pck As String In pm.GetInstalledPackages
       If pck.Contains("spotify") Then
           Dim name As String = pm.GetApplicationLabel(pck)
           Log(pck&"->"&name)
       End If
   Next
That did the job nicely, thanks.

Paul
 
Top