Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/189.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 单击链接时从Android WebView中的锚定标记获取href值_Javascript_Android_Webview_Android Webview - Fatal编程技术网

Javascript 单击链接时从Android WebView中的锚定标记获取href值

Javascript 单击链接时从Android WebView中的锚定标记获取href值,javascript,android,webview,android-webview,Javascript,Android,Webview,Android Webview,我正在WebView中加载网页。网页中有一个链接,桌面上会下载该文件,但在应用程序中,该链接会显示一个祝酒词,表示该应用程序已禁用该链接 我不确定单击链接时如何从锚定标记的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-

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

我不确定单击链接时如何从锚定标记的
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>
这种方法的问题是: 当我点击链接时,我额外获得了url。在这里以前都很好用。但是,从下一次开始,无论我在哪里单击webview,都会返回相同的额外内容。因此,即使我在点击url后点击了一个图像,我也会在额外的页面中得到相同的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。如何获取当前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

大部分工作可以在网页端本身完成。如果移动设备正在访问页面,您必须编写java脚本来识别哪个设备(移动设备、桌面等),然后使用java脚本绑定技术调用本机android代码来显示Toast消息

WebAppInterface.java

public class WebAppInterface {
    Context mContext;

    /** Instantiate the interface and set the context */
    WebAppInterface(Context c) {
        mContext = c;
    }

    /** Show a toast from the web page */
    @JavascriptInterface
    public void showToast(String toast) {
         Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
    }
}
YourHTML页面(此示例获得了一个按钮点击)


函数showAndroidToast(toast){
showToast(toast);
}

因为您使用的是WebView,而链接不是Java脚本,所以使用WebView客户端很容易实现这一点,您可以使用它来监视您的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;
    }
}
@覆盖
@TargetApi(Build.VERSION\u code.LOLLIPOP)
公共WebResourceResponse应该InterceptRequest(WebView视图、WebResourceRequest请求){
字符串url=request.getUrl().toString();
Log.d(“XXX”,“来自API21的Url应为interceptRequest:+Url”);
如果(url.contains(url)){
返回新的WebResourceResponse(“文本/html”、“UTF-8”、“不从应用程序下载”);
}否则{
返回null;
}
}
公共WebResourceResponse应InterceptRequest(WebView视图,字符串url){
Log.d(“XXX”,“来自shouldInterceptRequest的Url:+Url”);
如果(url.contains(url)){
返回新的WebResourceResponse(“文本/html”、“UTF-8”、“不从应用程序下载”);
}否则{
返回null;
}
}

很抱歉回复太晚。我从一开始就尝试过这种方法。但是,当我单击链接时,不会调用shouldOverrideUrlLoading。我获取url的唯一时间是我在问题代码示例中提到的在WebView上使用onTouchListener时。如果WebView客户端在WebView上设置正确,则获取url always导致调用该方法。如果是表单帖子,则情况并非如此,但您说的是锚定链接。您是否与触摸侦听器同时使用该方法?因为当您取消事件时,这当然不起作用。我使用WebViewClient更新了问题。请看一看。我没有同时使用这两种方法。首先,我尝试使用WebViewClient,但是,我无法从shouldOverrideUrlLoading获取url。因此,我尝试使用onTouchListener.private静态最终字符串url=“”;我正在shouldOverrideUrlLoading中记录它。@Override public boolean shouldOverrideUrlLoading(WebView视图,字符串url){Log.d(“xxx”,“url:+url”);if(url.contains(URL)){Log.d(“xxx”,“URL包含:”+URL);返回true;}返回false;}问题是当我单击链接时,我没有得到任何日志。没有URL或任何内容。使用LOGT更新问题此答案可能会有所帮助
<input type="button" value="Say hello" onClick="showAndroidToast('Hello  
             Android!')" />

    <script type="text/javascript">
    function showAndroidToast(toast) {
         Android.showToast(toast);
    }
</script>
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
    }

} );
@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;
    }
}