Android Code Snippet Get List Of Installed Apps and their icons

Discussion in 'Code Snippets' started by JakeBullet70, Sep 20, 2016.

  1. JakeBullet70

    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!

    Code:
    Private Sub ShowInstalledApps 'ignore

           
    Dim args(1As 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: Sep 20, 2016
  2. Erel

    Erel Administrator Staff Member Licensed User

    Another option:
    Code:
    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
     
    Procesor, Myr0n, KZero and 8 others like this.
  3. cambopad

    cambopad Active Member Licensed User

    Thanks for the code! I will need this in my upcoming project!
     
    JakeBullet70 likes this.
  4. JakeBullet70

    JakeBullet70 Well-Known Member Licensed User

    Can this code be modified to return icons too?
     
    Last edited: Sep 20, 2016
  5. inakigarm

    inakigarm Well-Known Member Licensed User

    Code:
    Dim imgv as Imageview

    .
    .
    .

    imgv.Background=pm.GetApplicationIcon(pck)
     
  6. JakeBullet70

    JakeBullet70 Well-Known Member Licensed User

    Good deal. @inakigarm I wish I found this 2 years ago. Would of saved me 20+ hours. :)
     
  7. taylorw

    taylorw Active Member Licensed User

  8. Erel

    Erel Administrator Staff Member Licensed User

    You can use this code to check whether it is a system app:
    Code:
    Sub IsSystemApp (PackageName As StringAs 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
     
    JakeBullet70 likes this.
  9. taylorw

    taylorw Active Member Licensed User

    Thanks for reply quickly @Erel, this code is helpful.;)
     
  10. Gunnar Daehling

    Gunnar Daehling Member Licensed User

    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 ?
     
  11. Erel

    Erel Administrator Staff Member Licensed User

    Can you post the full error message? What are you doing with the icons?
     
  12. Gunnar Daehling

    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:

    Code:
    Sub zeigeapps
        
    Dim args(1As 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
     

    Attached Files:

  13. Erel

    Erel Administrator Staff Member Licensed User

    Try this:
    Code:
    Dim bmp As Bitmap = icon.Bitmap
    icon.Initialize(bmp.Resize(
    100dip100dipTrue))
     
    Gunnar Daehling likes this.
  14. Wolfgang Trageiser

    Wolfgang Trageiser Member Licensed User

    how can i get the same information in B4i ? or is it impossible with IOS?
     
  15. Erel

    Erel Administrator Staff Member Licensed User

    You are not posting in the correct place.
     
  16. Gunnar Daehling

    Gunnar Daehling Member Licensed User

    Thank you very much.

    Best regards,
    Gunnar Dähling
     
  17. Gunnar Daehling

    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
     
  18. 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:

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

    Sub listapps
        
    Dim args(1As 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(
    50dip50dipTrue))
                    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: Oct 21, 2018
    Myr0n, DonManfred, Erel and 1 other person like this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice