Android Question Open External Applications

Peppe B

Active Member
Licensed User
I was wondering how I can open applications such as whatsapp, facebook, and other applications.

I know that with intent I can do this, but I do not know how to get the intent of each application.
 

Star-Dust

Expert
Licensed User
Longtime User
I was wondering how I can open applications such as whatsapp, facebook, and other applications.

I know that with intent I can do this, but I do not know how to get the intent of each application.
To get a list of the installed App and their packageName
B4X:
Public Sub ListApplication
    Dim args(1) As Object
    Dim Obj1, Obj2, Obj3 As Reflector
    Dim size, i, flags As Int
    Dim Types(1), Name, Pkn As String
   
 
    Obj1.Target = Obj1.GetContext
    Obj1.Target = Obj1.RunMethod("getPackageManager")
    Obj2.Target = Obj1.RunMethod2("getInstalledPackages", 0, "java.lang.int")
    size = Obj2.RunMethod("size")
    For i = 0 To size -1
        Obj3.Target = Obj2.RunMethod2("get", i, "java.lang.int")
        Pkn = Obj3.GetField("packageName")
        Obj3.Target = Obj3.GetField("applicationInfo")
        flags = Obj3.GetField("flags")
 
        If Bit.And(flags, 1)  = 0 Then
            args(0) = Obj3.Target
            Types(0) = "android.content.pm.ApplicationInfo"
            Name = Obj1.RunMethod4("getApplicationLabel", args, Types)
   
            Log("Name: " & Name)
            Log("PackageName:" & Pkn)
            'StartApp(Pkn)
        End If
    Next
   
End Sub
To start an App knowing the PackageName
B4X:
Public Sub StartApp(PackageName As String)

    Try
        Dim Intent1 As Intent
        Dim pm As PackageManager
        Intent1 = pm.GetApplicationIntent (PackageName)
        StartActivity (Intent1)
    Catch
        ToastMessageShow ("Error", True)
    End Try
End Sub

Requires the Phone and Reflector library
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Use PackageManager from the Phone library:
B4X:
Dim pm As PackageManager
For Each app As String In pm.GetInstalledPackages
   Log("Package: " & app)
   Log("Name: " & pm.GetApplicationLabel(app))
   Log("icon: " & pm.GetApplicationIcon(app)) 'drawable
   Log("intent: " & pm.GetApplicationIntent(app))
Next
 
Upvote 0
Top