Android Code Snippet isSystemapp

DonManfred

Expert
Licensed User
SubName: isSystemapp

Description: Check whether the given name is a Systemapp or not

B4X:
Sub isSystemapp(app As String) As Boolean
    Dim Obj1, Obj2, Obj3 As Reflector
  Dim size, i, flags As Int
  Dim result As Boolean = False

    Obj1.Target = Obj1.GetContext
  Obj1.Target = Obj1.RunMethod("getPackageManager") ' PackageManager
  Obj1.Target = Obj1.RunMethod2("getInstalledPackages", 0, "java.lang.int") ' List<PackageInfo>
  For i = 0 To Obj1.RunMethod("size") -1
      Obj2.Target = Obj1.RunMethod2("get", i, "java.lang.int") ' PackageInfo
    Dim name As String = Obj2.GetField("packageName")
        If app.ToLowerCase = name.ToLowerCase Then
            Obj3.Target = Obj2.GetField("applicationInfo") ' ApplicationInfo   
        flags = Obj3.GetField("flags")
        If Bit.AND(flags, 1)  = 0 Then
            'app is not in the system image
        Else
                ' Systemapp
                result = True
            End If   
        End If
  Next
    Return result
End Sub
Example
B4X:
Log("isSystemapp(com.whatsapp):"&isSystemapp("com.whatsapp"))
Log("isSystemapp(com.android.phone):"&isSystemapp("com.android.phone"))
** Activity (main) Create, isFirst = true **
isSystemapp(com.whatsapp):false
isSystemapp(com.android.phone):true
** Activity (main) Resume **

Dependencies: Reflection

Tags: Packagemanager, Systemapp
 

LucaMs

Expert
Licensed User
I enjoyed the "Spoiler": will have someone suspected that Whatsapp was a system app? :) (note: I do not have it anymore).
 

DonManfred

Expert
Licensed User
will have someone suspected that Whatsapp was a system app?
lol. No, not really... I just needed to pickup two apps to show how it works... for the systemappsample i used one that should be known by everyone (com.android.phone) and for the other one i just used Whatsapp as i got a message in that moment in Whatsapp :D
 

LucaMs

Expert
Licensed User
lol. No, not really... I just needed to pickup two apps to show how it works... for the systemappsample i used one that should be known by everyone (com.android.phone) and for the other one i just used Whatsapp as i got a message in that moment in Whatsapp
Sure, I get it, and you have chosen well... the strange thing is that you used the Spoiler... I think you did it because you enjoy it :)
 
Top