Android Code Snippet Get List Of Installed Apps and their icons

JakeBullet70

Well-Known Member
Licensed 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!

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:

Erel

Administrator
Staff member
Licensed 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
 

JakeBullet70

Well-Known Member
Licensed 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

Administrator
Staff member
Licensed 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
 

Gunnar Daehling

Member
Licensed 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 ?
 

Erel

Administrator
Staff member
Licensed User
Can you post the full error message? What are you doing with the icons?
 

Gunnar Daehling

Member
Licensed 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

Gunnar Daehling

Member
Licensed 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
 

Gunnar Daehling

Member
Licensed 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
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
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