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:

JakeBullet70

Well-Known Member
Licensed User
Longtime User
Private Sub ShowInstalledApps
Dim pm As PackageManager 'phone library
For Each pck As String In pm.GetInstalledPackages
Dim name As String = pm.GetApplicationLabel(pck)
Log(name)
Next
End Sub

Can this code be modified to return icons too?
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can use this code to check whether it is a system app:
B4X:
Sub IsSystemApp (PackageName As String) As Boolean
   Dim jo As JavaObject
   jo.InitializeContext
   Dim flags As Int = jo.RunMethodJO("getPackageManager", Null).RunMethodJO("getApplicationInfo", _
     Array (PackageName, 0)).GetField("flags")
   Return Bit.And(flags, 1) = 1 'FLAG_SYSTEM
End Sub
 

Tempomaster

Active Member
Licensed User
Longtime User
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!

I used this feature in an app. Unfortunately, it occasionally leads to an "out of memory" if I also list the apps of the system. I think that the icons are the cause. Is it really like that ?
 

Tempomaster

Active Member
Licensed User
Longtime User
Hello Erel,

the complete error message:

Error occurred on line: 541 (Main)
java.lang.ClassCastException: java.lang.OutOfMemoryError cannot be cast to java.lang.Exception
at anywheresoftware.b4a.agraham.reflection.Reflection.runmethod(Reflection.java:223)
at anywheresoftware.b4a.agraham.reflection.Reflection.RunMethod4(Reflection.java:857)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:710)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:339)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:139)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:170)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:166)
at anywheresoftware.b4a.shell.DebugResumableSub$RemoteResumableSub.resume(DebugResumableSub.java:19)
at anywheresoftware.b4a.keywords.Common$13.run(Common.java:1677)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5306)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)

The slightly modified subroutine:

B4X:
Sub zeigeapps
    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)
            lv1.AddTwoLinesAndBitmap2(name,"",icon.Bitmap,packName)
'            Log("-------------------------------------------------------------")
'            Log(name & " --> " & packName)
'            Log("-------------------------------------------------------------")
'        End If
    Next
    Label1.Text="Select the app for alarmsetting:"
    Label1.Invalidate
    Sleep(0)
    cancel.Enabled=True
End Sub

The line 541:

icon = Obj1.RunMethod4("getApplicationIcon", args, Types)

The error occurs very rarely on my device. For other users of the app, he always occurs. A screenshot of the app is in the attachment.

Best regards,
Gunnar Dähling
 

Attachments

  • Screenshot.png
    Screenshot.png
    31.9 KB · Views: 739

Tempomaster

Active Member
Licensed User
Longtime User
The problem is obviously solved. I've received several positive messages from other users of the app. Thanks again for the quick help.

Best regards,
Gunnar Daehling
 

Tempomaster

Active Member
Licensed User
Longtime User
Hello,

the subroutine in post #12 does not work if apps with adaptive icons are installed. On newer devices, the code is therefore no longer executable. I then changed the code and provide it below for everyone:

B4X:
Private lv1 As ListView
Private apps_cancel As Button
Private apps_Label1 As Label

Sub listapps
    Dim args(1) As Object
    Dim Obj1, Obj2, Obj3 As Reflector
    Dim size, i, flags As Int
    Dim Types(1), name,packName, name_temp As String
    Dim icon As BitmapDrawable
    Dim bmp As Bitmap '= icon.Bitmap
  
    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
        Try
            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")
      
            args(0) = Obj3.Target
            Types(0) = "android.content.pm.ApplicationInfo"
            name = Obj1.RunMethod4("getApplicationLabel", args, Types)
          
            name_temp=name.ToLowerCase
          
            If Bit.And(flags, 1) = 0 Or name_temp.Contains("map") Then
                'app is not in the system image
                icon = Obj1.RunMethod4("getApplicationIcon", args, Types)
                bmp = icon.Bitmap
          
                icon.Initialize(bmp.Resize(50dip, 50dip, True))
                lv1.AddTwoLinesAndBitmap2(name,"",icon.Bitmap,packName)
            End If
        Catch
            ' Error in the line "bmp = icon.Bitmap"
          
            bmp.InitializeMutable(50dip,50dip)
            Dim DestRect As Rect
            DestRect.Initialize(0,0,50dip,50dip)
            Dim can As Canvas
            can.Initialize2(bmp)
            Dim pm As PackageManager
            can.DrawDrawable(pm.GetApplicationIcon(packName),DestRect)
          
            icon.Initialize(bmp)
            lv1.AddTwoLinesAndBitmap2(name,"",icon.Bitmap,packName)
        End Try
    Next
  
    apps_Label1.Text="Select the app:"
    apps_Label1.Invalidate
    Sleep(0)
    apps_cancel.Enabled=True
End Sub
 
Last edited:

Paul Edwards

Member
Licensed User
Longtime User
Another option:
B4X:
Private Sub ShowInstalledApps
   Dim pm As PackageManager  'phone library
   For Each pck As String In pm.GetInstalledPackages
     Dim name As String = pm.GetApplicationLabel(pck)
     Log(name)
   Next
End Sub
How to check if a particular app is installed ie Spotify
 

DonManfred

Expert
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
 
Top