Open URL in preferred Browser if set, otherwise in stock browser

airblaster

Active Member
Licensed User
Longtime User
Hi,

I want to open a URL in the users preferred browser if he has one, and in the stock browser otherwise.
Currently I already have some code that opens URLs in stock browser or any other browser if no stock browser is found:
B4X:
Sub OpenBrowser(URL As String)
   Dim pm As PackageManager
   Dim i As Intent
   i.Initialize(i.ACTION_VIEW, URL)
   i.SetType("text/html")
   i.AddCategory("android.intent.category.BROWSABLE")
   Dim activities As List = pm.QueryIntentActivities(i)
   For Each cn As String In activities
      i.SetComponent(cn)
      If cn == "com.android.browser/.BrowserActivity" Then
         Exit
      End If
   Next
   StartActivity(i)
End Sub

So what's missing is to figure out whether the user has set a preferred browser and which.
Any ideas on how this could be achieved?
 

airblaster

Active Member
Licensed User
Longtime User
Figured out that this would probably involve PackageManager's getPreferredActivities, which currently isn't wrapped.
@Erel: Any chance this will be added to a future version of the phone library?
 
Upvote 0

Inman

Well-Known Member
Licensed User
Longtime User
Why don't you simply call StartActivity(i) right after i.Initialize(i.ACTION_VIEW, URL), without all the other code?
 
Upvote 0

airblaster

Active Member
Licensed User
Longtime User
Because that way the user would see a popup asking which browser to use, in case he hasn't set a preferred browser yet.
 
Upvote 0
Top