我的代码:
import kivy
from kivy.app import App
from kivy.lang import Builder
from kivy.utils import platform
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
WebView = autoclass('android.webkit.WebView')
WebViewClient = autoclass('android.webkit.WebViewClient')
activity = autoclass('org.renpy.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)
webview.getSettings().setJavaScriptEnabled(True)
wvc = WebViewClient();
webview.setWebViewClient(wvc);
activity.setContentView(webview)
webview.loadUrl('www.google.com')
class ServiceApp(App):
def build(self):
return Wv()
def on_pause(self):
return True
def on_resume(self):
return Wv()
if __name__ == '__main__':
ServiceApp().run()应用程序运行得很好,但我想在on_pause事件触发时保存当前的URL,然后在on_resume事件发生时返回到该URL。
我不知道该怎么做。
有什么建议吗?
发布于 2015-01-12 20:28:11
编辑:我很好奇,然后继续检查。实际上,我得到了一个java.lang.RuntimeException:可能检测到死锁,这是因为在UI线程被阻塞时在错误的线程上调用了WebView应用程序接口。有必要对WebViewClient进行子类化,但我不确定如何在jnius中做到这一点。
我想你可以毫无问题地访问你的url。窗口小部件树如下所示: ServiceApp -> Wv,但您没有使webview成为Wv的成员。您可能应该这样做:
@run_on_ui_thread
def create_webview(self, *args):
self.webview = WebView(activity)
self.webview.getSettings().setJavaScriptEnabled(True)
wvc = WebViewClient();
self.webview.setWebViewClient(wvc);
activity.setContentView(self.webview)
self.webview.loadUrl('www.google.com')在此之后,我认为您可以这样做:
class ServiceApp(App):
def build(self):
self.wv = Wv()
return wv
def on_pause(self):
# do something with url, I don't know the android API that well
# from http://developer.android.com/reference/android/webkit/WebView.html
self.wv.webview.getUrl()
return True
def on_resume(self):
# Here I have doubts why you create another widget but ok
self.wv = Wv()
return wv有许多我不确定的部分,需要测试以确保这样继续下去是安全的,但这是一个开始。我的两分钱。
https://stackoverflow.com/questions/26867124
复制相似问题