Android Question clickable list to launch another app [Solved]

trepdas

Active Member
Licensed User
Hello good people,
I have this Erel's example to list installed app.


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


can this list be clickable to launch the apps ?
if so, how ?

🙏
 

sfsameer

Expert
Licensed User
Longtime User
a small example of code, anyone?

No Problem :)

B4A:
Sub Globals
   
    Dim lst As ListView

End Sub
Sub Activity_Create(FirstTime As Boolean)

    lst.Initialize("lst")
    Activity.AddView(lst,0,0,100%x,100%y)
    ShowInstalledApps
end sub
Private Sub ShowInstalledApps
    Dim pm As PackageManager  'phone library
    For Each pck As String In pm.GetInstalledPackages
        Dim name As String = pm.GetApplicationLabel(pck)
        lst.AddSingleLine2(pm.GetApplicationLabel(pck),pm.GetApplicationIntent(pck))
    Next
End Sub
Sub lst_ItemClick (Position As Int, Value As Object)
    Try
        Dim in As Intent
        in = Value
        If in.IsInitialized Then StartActivity(in)
       
    Catch
        Log(LastException)
    End Try
End Sub
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
No Problem :)
Thank you
I don't think the code that Saif posted is correct. But then again who am I to argue with the powerhouse Saif (The Sword). The value returned by the item click in the listview should be the package name, then the intent code in the item click should be quite different from what you have.
This line:
B4X:
lst.AddSingleLine2(pm.GetApplicationLabel(pck),pm.GetApplicationIntent(pck))
should be:
B4X:
lst.AddSingleLine2(pm.GetApplicationLabel(pck),pck)
the Sub lst_ItemClick (Position As Int, Value As Object) code you have needs to change also.
It is also preferable to use an xCLV instead of Listview
 
Upvote 0

sfsameer

Expert
Licensed User
Longtime User
This line:
B4X:
lst.AddSingleLine2(pm.GetApplicationLabel(pck),pm.GetApplicationIntent(pck))
should be:
B4X:
lst.AddSingleLine2(pm.GetApplicationLabel(pck),pck)
the Sub lst_ItemClick (Position As Int, Value As Object) code you have needs to change also.
It is also preferable to use an xCLV instead of Listview

The code to run the package intent is :
B4A:
Dim in As Intent
Dim pm As PackageManager
in = pm.GetApplicationIntent("com.google.android.youtube")
If in.IsInitialized Then StartActivity(in)
StartActivity(in)

so what i did was put this line :
b4a:
pm.GetApplicationIntent("com.google.android.youtube")
in the value of the listview :
B4A:
lst.AddSingleLine2(pm.GetApplicationLabel(pck),pm.GetApplicationIntent(pck))

so when you want to run the package you dont have to do :
B4A:
Dim in As Intent
Dim pm As PackageManager
in = pm.GetApplicationIntent(value)
If in.IsInitialized Then StartActivity(in)
StartActivity(in)

you could just run :
B4X:
Dim in As Intent
        in = Value
        If in.IsInitialized Then StartActivity(in)

which produces the same result as your code above :)

I don't think the code that Saif posted is correct. But then again who am I to argue with the powerhouse Saif (The Sword).
it's an honor and pleasure to be considered that :)
 
Upvote 0

sfsameer

Expert
Licensed User
Longtime User
The correct code should be :
B4A:
Private Sub ShowInstalledApps
    Dim pm As PackageManager  'phone library
    For Each pck As String In pm.GetInstalledPackages
        Dim name As String = pm.GetApplicationLabel(pck)
        
    
        Dim in As Intent
        Dim pm As PackageManager
        in = pm.GetApplicationIntent(pck)
        If in.IsInitialized Then 
            lst.AddSingleLine2(pm.GetApplicationLabel(pck),in)
            
        End If
        
    Next
End Sub
 
Upvote 0

trepdas

Active Member
Licensed User
Thank you very much Mahares and sfsameer.
right after I tested it and it crashed I see here that the answer is already there !
🙏 🙏
 
Upvote 0
Top