Android 4.4.4 WebView onPageFinished() getTitle() returns 页面 URL?
Android 4.4.4 WebView in onPageFinished() getTitle() returns page URL?
我不知道是我误解了 jQueryMobile 还是看到了 Chromium Webview 错误。我有一个 Android 应用程序,它使用 webview 来显示 jQueryMobile 内容。当用户在页面的 DIV 内导航时,我在 onPageFinished() 中调用 getTitle() 来检索 Html 页面标题。有时,getTitle() return 是页面的 URL 而不是页面标题。
我不明白为什么。
这里是 Android activity 的简化版本,它演示了同样的问题:
public class WebViewTitleTestActivity extends Activity {
private WebView mWebView;
private TextView mTitleTextView;
private final String TAG = "WebViewTitleTest";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mWebView = (WebView) findViewById(R.id.webView);
mTitleTextView = (TextView) findViewById(R.id.title);
mWebView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
String titleText = view.getTitle();
Log.i(TAG, String.format("in onPageFinished. titleText = %s", titleText));
mTitleTextView.setText(String.format("Title: %s", titleText));
}
});
final WebSettings browserSettings = mWebView.getSettings();
browserSettings.setJavaScriptEnabled(true);
mWebView.loadUrl("file:///android_asset/jqmTest.html");
}
}
这是一些示例 HTML:
<html lang="en" class="ui-mobile">
<head>
<title>Send a message</title>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css"/>
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
</head>
<div history="false" data-role="page" data-cache="false" id="signal" data-url="signal" tabindex="0"
class="ui-page ui-body-c ui-page-active" style="min-height: 486px;">
<div data-role="content" class="content ui-content" role="main">
<h2>Send a message <a href="#help" class="information ui-link" id="signalInfoBox">(help)</a></h2>
<div class="text">
<p>We'll send a message to your destination.</p>
</div>
</div>
<div id="divAction" class="response">
<div class="buttons">
<div class="yes-single">
<a href="#" id="sendsignal" data-role="button" data-theme="c"
class="ui-btn ui-btn-up-c ui-btn-corner-all ui-shadow">
<span class="ui-btn-inner ui-btn-corner-all"><span class="ui-btn-text">Send</span></span></a>
</div>
</div>
</div>
</div>
<div data-role="page" class="ui-page ui-body-a ui-body-c" id="help" data-url="help" data-external-page="false"
tabindex="0" style="min-height: 526px;">
<div data-role="content" class="content ui-content" role="main">
<h2>More Info</h2>
<div class="text">
<p>You can send a message to your selected destination. Just click Send.</p>
</div>
</div>
<div class="response">
<div class="buttons">
<a id="btnCancel" href="#signal"
class="cancel-button ui-btn ui-btn-corner-all ui-shadow ui-btn-up-c ui-btn-up-undefined"
style="padding: 10px 0;" data-role="button">Close</a>
</div>
</div>
</div>
</html>
当用户单击(帮助)link 导航到#help 时,将显示帮助 div 并且 getTitle() 检索到的标题是正确的。当用户随后单击关闭按钮时,在 onPageFinished() 中调用 view.getTitle() return 页面 URL 而不是 Html 标题。
为什么 veiw.getTitle() return 页面 url?
我正在摩托罗拉 Moto E 运行 Android 4.4.4 上进行测试。目标 SDK 是 API 19。这似乎不会出现在 Android 4.4.2 或 5.x 上。这是 4.4.4 的已知问题吗?
由于 Android Chromium WebView 错误 (https://code.google.com/p/chromium/issues/detail?id=481570),我无法在 Android 4.4 及更高版本的 setWebChromeClient() 中使用 onReceivedTitle() 回调。相反,我使用 setWebViewClient() 的 onPageFinished() 回调创建了一个解决方法。然而,这在 Android 4.4.4 中似乎有一个错误,有时在调用 view.getTitle() 时返回页面 URL。
在页面中 DIV 之间的 jQueryMobile 导航中,以下有时 returns 页面 URL 而不是 Html 标题view.getTitle() 的调用
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
String titleText = view.getTitle();
}
作为解决方法,我输入了如下内容:
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
String javascript = "(function() { " +
"var titles = document.getElementsByTagName(\"title\");" +
"if (titles == null || titles.length != 1) return \"\";" +
"return titles[0].textContent; " +
"})();";
browser.evaluateJavascript(javascript, new ValueCallback<String>() {
@Override
public void onReceiveValue(String s) {
String htmlTitleString = s.replace("\"", "");
if (htmlTitleString != null && htmlTitleString.length() > 0) {
title.setText(htmlTitleString);
}
}
});
} else {
String titleText = view.getTitle();
if (titleText != null && titleText.length() > 0) {
title.setText(titleText);
}
}
}
这会从 KitKat 及更高版本的 WebView DOM 中提取 HTML 标题。到目前为止,这似乎是可靠的。我发现很难相信这是必要的,而且没有其他人偶然发现这些问题。我不觉得我们在做一些与众不同的事情。
我不知道是我误解了 jQueryMobile 还是看到了 Chromium Webview 错误。我有一个 Android 应用程序,它使用 webview 来显示 jQueryMobile 内容。当用户在页面的 DIV 内导航时,我在 onPageFinished() 中调用 getTitle() 来检索 Html 页面标题。有时,getTitle() return 是页面的 URL 而不是页面标题。
我不明白为什么。
这里是 Android activity 的简化版本,它演示了同样的问题:
public class WebViewTitleTestActivity extends Activity {
private WebView mWebView;
private TextView mTitleTextView;
private final String TAG = "WebViewTitleTest";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mWebView = (WebView) findViewById(R.id.webView);
mTitleTextView = (TextView) findViewById(R.id.title);
mWebView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
String titleText = view.getTitle();
Log.i(TAG, String.format("in onPageFinished. titleText = %s", titleText));
mTitleTextView.setText(String.format("Title: %s", titleText));
}
});
final WebSettings browserSettings = mWebView.getSettings();
browserSettings.setJavaScriptEnabled(true);
mWebView.loadUrl("file:///android_asset/jqmTest.html");
}
}
这是一些示例 HTML:
<html lang="en" class="ui-mobile">
<head>
<title>Send a message</title>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css"/>
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
</head>
<div history="false" data-role="page" data-cache="false" id="signal" data-url="signal" tabindex="0"
class="ui-page ui-body-c ui-page-active" style="min-height: 486px;">
<div data-role="content" class="content ui-content" role="main">
<h2>Send a message <a href="#help" class="information ui-link" id="signalInfoBox">(help)</a></h2>
<div class="text">
<p>We'll send a message to your destination.</p>
</div>
</div>
<div id="divAction" class="response">
<div class="buttons">
<div class="yes-single">
<a href="#" id="sendsignal" data-role="button" data-theme="c"
class="ui-btn ui-btn-up-c ui-btn-corner-all ui-shadow">
<span class="ui-btn-inner ui-btn-corner-all"><span class="ui-btn-text">Send</span></span></a>
</div>
</div>
</div>
</div>
<div data-role="page" class="ui-page ui-body-a ui-body-c" id="help" data-url="help" data-external-page="false"
tabindex="0" style="min-height: 526px;">
<div data-role="content" class="content ui-content" role="main">
<h2>More Info</h2>
<div class="text">
<p>You can send a message to your selected destination. Just click Send.</p>
</div>
</div>
<div class="response">
<div class="buttons">
<a id="btnCancel" href="#signal"
class="cancel-button ui-btn ui-btn-corner-all ui-shadow ui-btn-up-c ui-btn-up-undefined"
style="padding: 10px 0;" data-role="button">Close</a>
</div>
</div>
</div>
</html>
当用户单击(帮助)link 导航到#help 时,将显示帮助 div 并且 getTitle() 检索到的标题是正确的。当用户随后单击关闭按钮时,在 onPageFinished() 中调用 view.getTitle() return 页面 URL 而不是 Html 标题。
为什么 veiw.getTitle() return 页面 url?
我正在摩托罗拉 Moto E 运行 Android 4.4.4 上进行测试。目标 SDK 是 API 19。这似乎不会出现在 Android 4.4.2 或 5.x 上。这是 4.4.4 的已知问题吗?
由于 Android Chromium WebView 错误 (https://code.google.com/p/chromium/issues/detail?id=481570),我无法在 Android 4.4 及更高版本的 setWebChromeClient() 中使用 onReceivedTitle() 回调。相反,我使用 setWebViewClient() 的 onPageFinished() 回调创建了一个解决方法。然而,这在 Android 4.4.4 中似乎有一个错误,有时在调用 view.getTitle() 时返回页面 URL。
在页面中 DIV 之间的 jQueryMobile 导航中,以下有时 returns 页面 URL 而不是 Html 标题view.getTitle() 的调用
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
String titleText = view.getTitle();
}
作为解决方法,我输入了如下内容:
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
String javascript = "(function() { " +
"var titles = document.getElementsByTagName(\"title\");" +
"if (titles == null || titles.length != 1) return \"\";" +
"return titles[0].textContent; " +
"})();";
browser.evaluateJavascript(javascript, new ValueCallback<String>() {
@Override
public void onReceiveValue(String s) {
String htmlTitleString = s.replace("\"", "");
if (htmlTitleString != null && htmlTitleString.length() > 0) {
title.setText(htmlTitleString);
}
}
});
} else {
String titleText = view.getTitle();
if (titleText != null && titleText.length() > 0) {
title.setText(titleText);
}
}
}
这会从 KitKat 及更高版本的 WebView DOM 中提取 HTML 标题。到目前为止,这似乎是可靠的。我发现很难相信这是必要的,而且没有其他人偶然发现这些问题。我不觉得我们在做一些与众不同的事情。