kivy 使用webview加载网页

kivy app内访问网页 直接上源码, 

webview.py

from kivy.app import App

from kivy.uix.widget import Widget
from kivy.clock import Clock
from jnius import autoclass
from android.runnable import run_on_ui_thread

WebView = autoclass(‘android.webkit.WebView’)
WebViewClient = autoclass(‘android.webkit.WebViewClient’)
#activity = autoclass(‘org.renpy.android.PythonActivity’).mActivity
activity = autoclass(‘org.kivy.android.PythonActivity’).mActivity

class Wv(Widget):
def __init__(self, **kwargs):
super(Wv, self).__init__(**kwargs)
Clock.schedule_once(self.create_webview, 0)

@run_on_ui_thread
def create_webview(self, *args):
webview = WebView(activity)
settings = webview.getSettings()
settings.setJavaScriptEnabled(True)
settings.setUseWideViewPort(True) # enables viewport html meta tags
settings.setLoadWithOverviewMode(True) # uses viewport
settings.setSupportZoom(True) # enables zoom
settings.setBuiltInZoomControls(True) # enables zoom controls
wvc = WebViewClient()
webview.setWebViewClient(wvc)
activity.setContentView(webview)
webview.loadUrl(‘http://beta.html5test.com’)

class ServiceApp(App):
def build(self):
return Wv()

if __name__ == ‘__main__’:
ServiceApp().run()

 

.p4a

–dist_name py2dist
–android_api 23
–sdk_dir /home/kivydev/andr/android-sdk-linux
#–ndk_dir /home/kivydev/andr/android-ndk-r10e
–ndk_dir /home/kivydev/andr/android-ndk-r13
–ndk_version r13

–icon /home/kivydev/work/xyapp/res/10.png
–presplash /home/kivydev/work/xyapp/res/10.png

–requirements pyjnius,python2,kivy,sdl2_mixer,android
–private src
–package com.xyapp
–name xyapp
–version 1.0
–bootstrap sdl2

–arch armeabi-v7a
–copy-libs
–window
–permission INTERNET
–permission DELETE_CACHE_FILES
–permission VIBRATE
–permission ACCESS_WIFI_STATE
–permission CHANGE_WIFI_STATE
–permission CHANGE_NETWORK_STATE

 

参考:

https://github.com/kivy/kivy/wiki/Android-native-embedded-browser

https://github.com/linuxrootok/kivy-webview

发表评论

您的电子邮箱地址不会被公开。