gpt4 book ai didi

javascript - 单击链接时从 Android WebView 中的 anchor 标记获取 href 值

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:46:06 28 4
gpt4 key购买 nike

我正在 WebView 中加载网页。网页中有一个链接,在桌面上会下载该文件,但在应用程序中,该链接应显示一个 Toast,表示该链接已为该应用程序禁用。

我不确定如何在单击链接时从 anchor 标记的 href 获取值。

<a class="btn btn-primary" download="http://xx.xxx.com/wp-content/uploads/2015/11/abc-27-15.mp3" href="http://xx.xxx.com/wp-content/uploads/2015/11/abc-27-15.mp3">
<i class="fa fa-download"></i> Download Audio</a>

有人可以分享关于如何执行此操作的想法或任何示例代码。

编辑:1

这是我目前正在做的事情:

private static final String URL = "http://xx.xxx.com/wp-content/uploads/";

webView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {

if (event.getAction() == MotionEvent.ACTION_DOWN) {

WebView.HitTestResult hr = ((WebView) v).getHitTestResult();
String extra = hr.getExtra();

if (extra != null && extra.startsWith(URL) && extra.endsWith(".mp3")) {
Log.d("WebviewActivity", "Extra: " + extra);
Log.d("WebviewActivity", "Contains URL");

return true;
}
}

return false;
}
});

这种方法的问题是:当我点击链接时,我得到了额外的网址。到这里为止一切正常。但是,从下一次开始,无论我在哪里点击 webview,都会返回相同的额外内容。因此,即使我在单击 url 后单击图像,我也会在 extra 中获得相同的 url。不确定我是否做错了什么。或者这是正确的方法。

如果您需要任何详细信息,请告诉我。

编辑:2

private Handler mHandler = new Handler() {

@Override
public void handleMessage(Message msg) {
// Get link-URL.
String url = (String) msg.getData().get("url");

// Do something with it.
if (url != null) {
Log.d(TAG, "URL: "+url);
}
}
};



webView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {

if (event.getAction() == MotionEvent.ACTION_DOWN) {

WebView.HitTestResult hr = ((WebView) v).getHitTestResult();


if (hr.getType() == WebView.HitTestResult.SRC_ANCHOR_TYPE) {

Message msg = mHandler.obtainMessage();
webView.requestFocusNodeHref(msg);
}


}

return false;
}
});



webView.loadUrl(mUrl);

}

现在,我得到了在最后一个 action_down 事件中被点击的 URL。如何获取当前网址?

编辑 3(尝试使用 webviewclient:

 private class MyWebViewClient extends WebViewClient {

private static final String URL = "xx.xxx.com/wp-content/uploads/";

@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);

if (!isFinishing())
mProgressDialog.show();
}

@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
mProgressDialog.dismiss();
}

@Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);

Toast.makeText(WebviewActivity.this,
"Please check your internet " + "connection and try again",
Toast.LENGTH_SHORT).show();
}


@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {

Log.d("xxx", "Url: " + url);

if(url.contains(URL)) {
Log.d("xxx", "Url Contains: " + URL);
return true;
}

return false;
}

}


mMyWebViewClient = new MyWebViewClient();
webView.setWebViewClient(mMyWebViewClient);

点击链接时在logcat中的输出:

03-01 15:38:19.402 19626-19626/com.xx.xxx D/cr_Ime: [ImeAdapter.java:553] focusedNodeChanged: isEditable [false]
03-01 15:38:19.428 19626-19626/com.xx.xxx D/cr_Ime: [ImeAdapter.java:253] updateKeyboardVisibility: type [0->0], flags [0], show [true],
03-01 15:38:19.428 19626-19626/com.xx.xxx D/cr_Ime: [ImeAdapter.java:326] hideKeyboard
03-01 15:38:19.429 19626-19626/com.xx.xxx D/cr_Ime: [InputMethodManagerWrapper.java:56] isActive: true
03-01 15:38:19.429 19626-19626/com.xx.xxx D/cr_Ime: [InputMethodManagerWrapper.java:65] hideSoftInputFromWindow

最佳答案

因为您使用的是 WebView 并且链接不是 Java 脚本,所以使用 WebViewClient 可以很容易地实现这一点,您可以使用它来监视您的 WebView

myWebView.setWebViewClient( new WebViewClient() {

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// check something unique to the urls you want to block
if (url.contains("xx.xxx.com")) {
Toast.make... //trigger the toast
return true; //with return true, the webview wont try rendering the url
}
return false; //let other links work normally
}

} );

可能是因为您的 URL 以 .mp3 结尾,所以该文件被视为资源。您还应该覆盖 WebViewClient 的 shouldInterceptRequest 方法来检查这一点。

@Override
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
String url = request.getUrl().toString();
Log.d("XXX", "Url from API21 shouldInterceptRequest : " + url);
if (url.contains(URL)) {
return new WebResourceResponse("text/html", "UTF-8", "<html><body>No downloading from app</body></html>");
} else {
return null;
}
}

public WebResourceResponse shouldInterceptRequest (WebView view, String url) {
Log.d("XXX", "Url from shouldInterceptRequest : " + url);
if (url.contains(URL)) {
return new WebResourceResponse("text/html", "UTF-8", "<html><body>No downloading from app</body></html>");
} else {
return null;
}
}

关于javascript - 单击链接时从 Android WebView 中的 anchor 标记获取 href 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35520839/

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