Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
mrhdias opened this issue Jul 19, 2016 · 5 comments
Closed

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

mrhdias opened this issue Jul 19, 2016 · 5 comments

Comments

@mrhdias
Copy link
Contributor

mrhdias commented Jul 19, 2016

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'
@mrhdias
Copy link
Contributor Author

mrhdias commented Jul 22, 2016

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

@WillDHB
Copy link

WillDHB commented Jul 27, 2016

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.

@inclement
Copy link
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.

@inclement
Copy link
Member

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

@mrhdias
Copy link
Contributor Author

mrhdias commented Oct 11, 2016

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants