kil*_*bee 5 python android webview kivy
我已经按照 Kivy wiki 说明成功地将 WebView 附加到我的 Kivy 应用程序。它按预期工作,但我想断开连接并返回到我正常的 Kivy ui。我该怎么做?
我试图探索 WebView文档,访问它的方法(WebView.destroy()抱怨破坏仍然附加的 WebView),它是父方法(我什至不确定这是否是要走的路),但我不能不要摆脱 WebView。
好吧,我不确定这是否是最好的解决方案,或者是否足够干净,但我知道这是唯一可行的解决方案。虽然它可以工作并且看起来很稳定,但它需要由更了解 Kivy 和 Android API 本身的人进行进一步测试。
if platform == 'android':
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
else:
import webbrowser
def run_on_ui_thread(func):
''' just for desktop compatibility '''
return func
class MyScreen(Screen):
view_cached = None # make these object properties?
webview = None
wvc = None # not even needed probably
code = StringProperty() # this property triggers webview to close
url_to_load = None
def on_enter(self):
if platform == 'android':
Clock.schedule_once(self.create_webview, 0) # probably doesn't need clocked call (because decorators will make sure
# function runs on correct thread), but leaving it until tested properly
else:
webbrowser.open_new(self.url_to_load) # on desktop just run the webbrowser
@run_on_ui_thread
def on_code(self, *args):
''' runs when you are ready to detach WebView '''
self.detach_webview()
@run_on_ui_thread
def create_webview(self, *args):
''' attaching webview to app '''
if self.view_cached is None:
self.view_cached = activity.currentFocus # caches current view (the one with kivy) as a view we want to go back to; currentFocus or getCurrentFocus() works
self.webview = WebView(activity)
settings = self.webview.getSettings()
settings.setJavaScriptEnabled(True) # enables js
settings.setUseWideViewPort(True) # enables viewport html meta tags
settings.setLoadWithOverviewMode(True) # uses viewport
settings.setSupportZoom(True) # enables zoom
settings.setBuiltInZoomControls(True) # enables zoom controls
self.wvc = WebViewClient()
self.webview.setWebViewClient(self.wvc)
activity.setContentView(self.webview)
self.webview.loadUrl(self.url_to_load)
@run_on_ui_thread
def key_back_handler(self, *args):
''' sketch for captured "key back" event (in App), not tested properly '''
if self.webview:
if self.webview.canGoBack() == True:
self.webview.goBack()
else:
self.detach_webview()
Clock.schedule_once(self.quit_screen, 0)
else:
App.get_running_app().root.current = 'some_other_screen_to_switch_to'
@run_on_ui_thread
def detach_webview(self, *args):
if self.webview:
self.webview.clearHistory()
self.webview.clearCache(True)
self.webview.loadUrl("about:blank")
self.webview.freeMemory() # probably not needed anymore
self.webview.pauseTimers() # this should stop any playing content like videos etc. in the background; probably not needed because of 'about:blank' above
activity.setContentView(self.view_cached) # sets cached view as an active view
#self.webview = None # still needs testing;
#self.wvc = None
@mainthread
def quit_screen(self, *args):
''' if not called on @mainthread, it will be freezed '''
app = App.get_running_app()
app.root.current = 'some_other_screen_to_switch_to'
Run Code Online (Sandbox Code Playgroud)
我在进入 MyScreen(Screen) 时创建 WebView,并在分离 WebView 时切换回其他屏幕。
WebView 被缓存之前的视图(这有效吗?可能会更好以其他方式访问它)并在 WebView 被销毁时再次使用。quit_screen() 调用可能应该移至 detach_webview(),但整个代码可能需要更好的组织,因此保持原样,因为这是经过测试的示例。
| 归档时间: |
|
| 查看次数: |
803 次 |
| 最近记录: |