Android Question [SOLVED] How to determine default browser on device

JackKirk

Well-Known Member
Licensed User
Longtime User

JackKirk

Well-Known Member
Licensed User
Longtime User
Erel, I installed Firefox on my Android device and explicitly made it the default browser and then installed your code asis.

When I run my B4A code in Debug mode the line:

B4X:
mInfo = r.RunMethod4("resolveActivity", Array As Object(In, 0), _
      Array As String("android.content.Intent", "java.lang.int"))

just produces null.
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
Test
Log(GetDefaultBrowser)

B4X:
'Use library JavaObject
Public Sub GetDefaultBrowser As String
    Dim Result As String
    
    Dim jo As JavaObject
    jo.InitializeContext
    
    Dim intent As JavaObject
    intent.InitializeNewInstance("android.content.Intent", Array("android.intent.action.VIEW"))

    Dim uri As JavaObject
    uri.InitializeNewInstance("android.net.Uri", Array("http://"))
    intent.RunMethod("setData", Array(uri))

    Dim pm As JavaObject
    pm = jo.RunMethod("getPackageManager", Null)

    Dim resolveInfo As JavaObject
    resolveInfo = pm.RunMethod("resolveActivity", Array(intent, 0))

    If resolveInfo.IsInitialized Then
        Dim activityInfo As JavaObject
        activityInfo = resolveInfo.RunMethod("activityInfo", Null)
        Result = activityInfo.GetField("packageName")
    Else
        Result = "No default browser found."
    End If

    Return Result
End Sub
 
Upvote 0

toby

Well-Known Member
Licensed User
Longtime User
Test
Log(GetDefaultBrowser)

B4X:
'Use library JavaObject

    uri.InitializeNewInstance("android.net.Uri", Array("http://"))

End Sub
I tested your code as it is and the above line of code caused the "java.lang.RuntimeException: Constructor not found" error
Logger connected to: samsung SM-A115W

** Activity (main) Pause event (activity is not paused). **
** Service (starter) Destroy (ignored)**
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create (first time) **
*** mainpage: B4XPage_Created
*** mainpage: B4XPage_Appear
** Activity (main) Resume **
Error occurred on line: 42 (B4XMainPage)
java.lang.RuntimeException: Constructor not found.
at anywheresoftware.b4j.object.JavaObject.InitializeNewInstance(JavaObject.java:95)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:777)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:354)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:157)
at anywheresoftware.b4a.debug.Debug.delegate(Debug.java:262)
at ddg.defaultbrowser.b4xmainpage._getdefaultbrowser(b4xmainpage.java:75)
at ddg.defaultbrowser.b4xmainpage._button1_click(b4xmainpage.java:66)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:157)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:201)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
at android.view.View.performClick(View.java:7792)
at android.widget.TextView.performClick(TextView.java:16112)
at android.view.View.performClickInternal(View.java:7769)
at android.view.View.access$3800(View.java:910)
at android.view.View$PerformClick.run(View.java:30218)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:226)
at android.os.Looper.loop(Looper.java:313)
at android.app.ActivityThread.main(ActivityThread.java:8669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1135)
 
Upvote 0

JackKirk

Well-Known Member
Licensed User
Longtime User
I tested your code as it is and the above line of code caused the "java.lang.RuntimeException: Constructor not found" error
Same here.

Digging a bit in the forums for "java.lang.RuntimeException: Constructor not found" I came across this:

https://www.b4x.com/android/forum/t...ion-constructor-not-found.125252/#post-790796

from which I am guessing it is because we are both not using B4XPages but TILogistic is.

The project I am wishing to put this in is huge and predates B4XPages so I am stuck.
 
Upvote 0

JackKirk

Well-Known Member
Licensed User
Longtime User
I've solved it with a bit more searching of the forum - eventually found this:

https://www.b4x.com/android/forum/t...r-query_all_packages-on-sdk30.135863/#content

Erel's original 2011 code at:

https://www.b4x.com/android/forum/t...-a-particular-type-of-intent.13580/post-76825

works fine once you add this to the manifest:
B4X:
AddManifestText(<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"/>)

but publishing may be a bear:

https://support.google.com/googleplay/android-developer/answer/10158779
 
  • Like
Reactions: byz
Upvote 0
Top