如何处理的WebView回调 [英] How to handle callbacks in webview

查看:156
本文介绍了如何处理的WebView回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个机器人项目,现在,有一个关于如何做回调不同webviews问题。我用JSInterface为我的项目了。在这里,我有2 webviews。其中有一个索引页,花药是覆盖(仍然是一个html页面虽然。)如果有用户点击上覆盖一些链接,它应该解雇这是写在java文件中的回调函数,我想要做的就是那里的索引页连接到通过JSInterface。这听起来令人困惑,但我有画点什么,以帮助它清楚!

I am working on an android project right now and have a question about how to do callbacks in different webviews. I used JSInterface for my project too. Here I have 2 webviews. One has an index page, anther is a overlay(still a html page though.) What I want to do is if any user clicks on some links on the overlay, it should fire a callback function which is written in the java file where the index page was connected to through JSInterface. It might sound confusing, but I have draw something to help make it clear!

谢谢!

Thanks!

推荐答案

您可以像使用myurl自定义URL方案://功能,为您的功能链接。然后写一个事件处理程序的WebView的shouldOverrideUrlLoading事​​件中你决定如何处理URL:要么指示的WebView加载它,或者做一些自定义操作

You can use a custom URL scheme like myurl://function for your functionality links. Then write an event handler for the WebView's shouldOverrideUrlLoading event in which you decide how to process the URL: either instruct the webview to load it, or do some custom action.

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)  
{  
    if (url.startsWith("myurl://"))  
    {  
        // Parse further to extract function and do custom action  
    }
    else  
    {  
        // Load the page via the webview
        view.loadUrl(url);  
    }  
    return true;  
}  

我用startsWith查询的网址这个快速和肮脏的例子,但是你应该考虑使用android.net.Uri.parse解析的网址。

I used startsWith to check the URL for this quick and dirty example, but you should consider using android.net.Uri.parse for parsing URLs.

这应该允许您调用Java函数foo(),而不必通过第一web视图。

This should allow you to call the Java function foo() without having to go through the first WebView.

如果你想通过第一web视图,那么你可以呼吁JSInterface这样的功能(其中webView1是第一的WebView通过findViewById检索):

If you want to go through the first webview, then you can call a function on the JSInterface like this (where webView1 is the first WebView retrieved through findViewById):

webView1.loadUrl("javascript:myjsinterface.myjsfunc();")

这篇关于如何处理的WebView回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆