Android 一旦附加,如何取消附加webview?
我已成功将WebView连接到我的Kivy应用程序。它按预期工作,但我想退出并返回到我正常的Kivy ui。我该怎么做Android 一旦附加,如何取消附加webview?,android,python,webview,kivy,Android,Python,Webview,Kivy,我已成功将WebView连接到我的Kivy应用程序。它按预期工作,但我想退出并返回到我正常的Kivy ui。我该怎么做 我曾尝试探索WebView,访问它的方法(抱怨破坏了一个仍然连接的WebView),它的父方法(我甚至不确定这是不是一种方法),但我无法摆脱WebView。好的,我不确定这是不是最好的解决方案,或者说不够干净,但我知道这是唯一有效的解决方案。虽然它工作正常并且看起来很稳定,但它需要更了解Kivy和Android API本身的人进行进一步测试 if platform == 'an
我曾尝试探索WebView,访问它的方法(抱怨破坏了一个仍然连接的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'
我在进入MyScreen(Screen)时创建WebView,在分离WebView时,切换回其他屏幕
WebView被缓存之前的视图(这样做有效吗?最好用其他方式访问它)并在WebView被销毁时再次使用。
quit_screen()调用可能应该移动到detach_webview(),但是代码作为一个整体可能需要更好的组织,所以保持原样,因为这是测试示例。使用webview.loadUrl(“about:blank”)可靠地重置视图状态并释放页面资源(包括任何正在运行的JavaScript)。这能让你破坏网络视图吗?我对Kivy不太了解,但你可以看看事实上我已经找到了解决这个问题的方法,我不确定这是不是最好的方法,但确实有效。实际上,销毁webview本身并不是一个问题,问题在于应用程序活动被webview“拥有”(finish(),destroy()方法实际上会杀死整个应用程序,而不仅仅是webview),因此需要与android.runnable和kivy的主线程的ui线程进行一些处理。正如我所说,我不确定这是最好的方式,但这是我所知道的唯一方式。等我有时间的时候,我会很快发布答案。一个有效的例子