Skip to content

webbrowser.open() doesn't work on Android with bootstrap=sdl2 #846

Closed

Description

@mrhdias

webbrowser.open() doesn't work on Android with bootstrap=sdl2, but work with pygame.
No error, no crash, just nothing happens. Is there any solution to fix this issue?

Android Version: 6.0.1

import webbrowser
webbrowser.open('http:/www.kivy.org/')

From buildozer logs

# Run 'python -m pythonforandroid.toolchain
 --color=always
 --storage-dir=/home/hdias/Work/mobile_app/.buildozer/android/platform/build create
 --dist_name=mobile_app 
 --bootstrap=sdl2
 --requirements=pycrypto,plyer==master,kivy==master --arch armeabi-v7a --copy-libs'

Activity

  1. mrhdias commented on Jul 22, 2016

    @mrhdias
    ContributorAuthor

    To fix this error, I add the android module to the requirements in buildozer.spec.

  2. WillDHB commented on Jul 27, 2016

    @WillDHB

    I believe you also need to do an "import android" prior to calling webbrowser.open() (unless something else is automatically importing), as the android module registers the AndroidBrowser with the webbrowser module.

  3. inclement commented on Jul 27, 2016

    @inclement
    Member

    For reference, the specific code you need is as below (you can use this instead of the android module if you like):

    from pyjnius import autoclass
    
    def open_url(url):
        Intent = autoclass('android.content.Intent')
        Uri = autoclass('android.net.Uri')
        browserIntent = Intent()
        browserIntent.setAction(Intent.ACTION_VIEW)
        browserIntent.setData(Uri.parse(url))
        currentActivity = cast('android.app.Activity', mActivity)
        currentActivity.startActivity(browserIntent)
    
    # Web browser support.
    class AndroidBrowser(object):
        def open(self, url, new=0, autoraise=True):
            open_url(url)
        def open_new(self, url):
            open_url(url)
        def open_new_tab(self, url):
            open_url(url)
    
    import webbrowser
    webbrowser.register('android', AndroidBrowser, None, -1)
    

    I'd like to add this to the doc somehow, but am not sure where to put it.

  4. inclement commented on Jul 27, 2016

    @inclement
    Member

    I've added some brief documentation for this, and we're discussing how to make it easily/automatically available by default.

  5. mrhdias commented on Oct 11, 2016

    @mrhdias
    ContributorAuthor

    Thanks for the clues, but your solution does not work because it is not complete.
    Can you fix the code with this?
    http://python-for-android.readthedocs.io/en/latest/apis/

    from kivy.utils import platform
    
    def launch_webbrowser(url):
        import webbrowser
        if platform == 'android':
            from jnius import autoclass, cast
            def open_url(url):
                PythonActivity = autoclass('org.kivy.android.PythonActivity')
                activity = PythonActivity.mActivity
                Intent = autoclass('android.content.Intent')
                Uri = autoclass('android.net.Uri')
                browserIntent = Intent()
                browserIntent.setAction(Intent.ACTION_VIEW)
                browserIntent.setData(Uri.parse(url))
                currentActivity = cast('android.app.Activity', activity)
                currentActivity.startActivity(browserIntent)
    
            # Web browser support.
            class AndroidBrowser(object):
                def open(self, url, new=0, autoraise=True):
                    open_url(url)
                def open_new(self, url):
                    open_url(url)
                def open_new_tab(self, url):
                    open_url(url)
    
            webbrowser.register('android', AndroidBrowser, None, -1)
    
        webbrowser.open(url)
    
    launch_webbrowser('http://www.kivy.org')
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions