gpt4 book ai didi

javascript - 我从模拟器上的 webview 获取源代码,但无法在真实设备上获取源代码? - 安卓

转载 作者:行者123 更新时间:2023-11-28 08:27:30 26 4
gpt4 key购买 nike

代码可以在模拟器上运行,但不能在真实设备上运行。

classJavaScriptInterface{
private TextView contentView;
public JavaScriptInterface(TextView aContentView){
contentView = aContentView;
}
@SuppressWarnings("unused")
public void processContent(String aContent){
final String content = aContent;
contentView.post(new Runnable(){
public void run(){
contentView.setText(content);
contentView.setTextColor(Color.DKGRAY);
}
});
}
}

TextView tv2 = (TextView) findViewById(R.id.source);
webview1.addJavascriptInterface(new JavaScriptInterface(tv2), "INTERFACE");

webview1.setWebViewClient(new WebViewClient(){
@Override
public void onPageFinished(WebView view, String url)
{
view.loadUrl("javascript:window.INTERFACE.processContent(document.getElementsByTagName('body')[0].innerText);");
}
});

最佳答案

根据我的经验,onPageFinished() 并不总是了解页面是否真正完成的可靠方法。根据您加载的内容,调用 onPageFinished() 后,可能会发生其他重定向或帧。有关更多详细信息,请参阅其他 SO 帖子:How to determine when Android WebView is completely done loading?

您可以通过在 onPageFinished 中添加延迟来快速检查是否存在问题:

@Override   
public void onPageFinished(final WebView view, String url)
{
view.postDelayed(new Runnable()
{
public void run()
{
view.loadUrl("javascript:window.INTERFACE.processContent(document.getElementsByTagName('body')[0].innerText);");
}
}, 5000);
}

如果它适用于延迟,您应该使用另一篇文章中描述的方法来解决问题(不要在最终产品中使用延迟 5 秒的 postDelayed)。

关于javascript - 我从模拟器上的 webview 获取源代码,但无法在真实设备上获取源代码? - 安卓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22239838/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com