0

我正在开发 web + android 项目,但问题是我无法从 shouldOverrideUrlLoading 获取 url。我认为这是因为有嵌套的 html 视图和动态数据分配到 html 容器中。html web 视图中有滑动菜单,它与 facebook、gmail 类型的滑动菜单相同,但它在 html 中。当您点击该菜单的任何项目时,我无法获取 url ..(我只想确定用户点击了哪个按钮或菜单项)。

如果我尝试简单的href链接而不是它的工作我得到shouldOverrideUrlLoading url。

****我认为我的网络开发人员加载了一个索引页面并在其中放置了一个 div 标签,当您从一个页面移动到另一个页面时,他只是更改了 div 标签的内容,这就是为什么我没有在 shouldOverrideUrlLoading 中获取 url 是还有其他方法可以检索用户单击按钮或项目****

是否有任何方法可以跟踪嵌套 html 页面的加载。

这是嵌套 html 、 css 、 js 的索引页面

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="msapplication-tap-highlight" content="no">
        <title>Application</title>
        <!--
        <?php
            // Use that to serve the correct css file to the os using the app
            $agent = $_SERVER['HTTP_USER_AGENT'];
        ?>
        -->

        <link rel="stylesheet" href="css/all.css">
        <script data-main="js/all" src="js/libs/require/require.min.js"></script>
    </head>

    <body>
        <div id="page"></div>

    </body>

</html>

这是滑动菜单

<ul class="left-navigation list">
    <li class="niv1"><h3><a href="">Accueil</a></h3></li>
    <li class="niv1"><h3>Votre compte</h3></li>
    <li class="niv1"><h3><a href="#add-video">Proposer une vidéo</a></h3></li>
    <li class="niv1"><h3>Vidéos</h3>
        <ul>
            <li class="niv2"><h4><a href="#video-list">Populaires</a></h4></li>
            <li class="niv2"><h4><a href="#video-list">+ commentées</a></h4></li>
            <li class="niv2"><h4><a href="#video-list">Dernières vidéos</a></h4></li>
        </ul>
    </li>
    <li class="niv1"> <h3>Concours</h3>
        <ul>
            <li class="niv2"><h4><a href="#contest-list">Tous</a></h4></li>
            <li class="niv2"><h4><a href="#contest-opened">En cours</a></h4></li>
            <li class="niv2"><h4><a href="#contest-closed">Terminé&lt;/a></h4></li>
        </ul>
    </li>
</ul>
<ul class="right-navigation list">
    <li class="niv1 clearfix"><h3>Secteurs</h3>
        <ul>
            {{#secteurs}}
            <li class="niv2"><h4><a href="#sector{{secteur_id}}">{{nom_secteur}}</a></h4></li>
            {{/secteurs}}
        </ul>
    </li>
</ul>

安卓代码

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    myWebView = (WebView) findViewById(R.id.webview1);
    myWebView.setWebChromeClient(new WebChromeClient());
    HelloWebViewClient webViewClient = new HelloWebViewClient(); 
    myWebView.setWebViewClient( webViewClient);
    myWebView.getSettings().setAllowUniversalAccessFromFileURLs(true);

    WebSettings webSettings = myWebView.getSettings();

    webSettings.setJavaScriptEnabled(true);
    webSettings.setDomStorageEnabled(true);

    myWebView.loadUrl("file:///android_asset/index.html");

}    

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

private class HelloWebViewClient extends WebViewClient{ 
    @Override 
    public boolean shouldOverrideUrlLoading(WebView view, String url){
        Log.i("shouldOverrideUrlLoading", url);
        return true;
    }
}
4

1 回答 1

-1

将此代码放在加载网址之前

    webView.setWebViewClient(new Callback());
    webView.setWebChromeClient(new webChromeClient());
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setDatabaseEnabled(true);
    webView.getSettings().setDomStorageEnabled(true);
    webView.getSettings().setSavePassword(true);
    webView.getSettings().setAllowFileAccess(true);
    webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    webView.getSettings().setSupportMultipleWindows(true);
    webView.getSettings().setAppCacheMaxSize(1024*1024*16);
    String appCachePath = getApplicationContext().getCacheDir().getAbsolutePath();
    webView.getSettings().setAppCachePath(appCachePath);
    webView.getSettings().setAppCacheEnabled(true);
    webView.getSettings().setRenderPriority(RenderPriority.HIGH);
    webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
    String databasePath = "/data/data/" + getApplicationContext().getPackageName() + "/databases/";


    webView.getSettings().setDatabasePath(databasePath);
    //webView.getSettings().setGeolocationEnabled(true);
    webView.getSettings().setSaveFormData(true);
    webView.getSettings().setPluginsEnabled(true);
    webView.getSettings().setLoadWithOverviewMode(true);
    JavascriptInterface javasriptInterface = new JavascriptInterface(MainActivity.this);
    webView.addJavascriptInterface(javasriptInterface, "Print");


private class Callback extends WebViewClient{  
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        AppLog.logString(TAG+"shouldOverrideUrlLoading");
        AppLog.logString(TAG+"url: "+url);

        view.loadUrl(url);
        }
        return true;
        //return (false);
    }
    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        // TODO Auto-generated method stub
        AppLog.logString(TAG+"onPageStarted");
        AppLog.logString(TAG+"url: "+url);

        super.onPageStarted(view, url, favicon);
    }   

    @Override
    public void onPageFinished(WebView view, String url) {
        // TODO Auto-generated method stub
        AppLog.logString(TAG+"onPageFinished");
        AppLog.logString(TAG+"url: "+url);

        super.onPageFinished(view, url);
    }   
 }
 private class webChromeClient extends WebChromeClient{
     @Override
        public void onExceededDatabaseQuota(String url, String databaseIdentifier, long currentQuota, long estimatedSize, long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) { 
         AppLog.logString(TAG+"onExceededDatabaseQuota");
         quotaUpdater.updateQuota(5 * 1024 * 1024); 
     }      
     @Override
     public void onProgressChanged(WebView view, int newProgress) {
        MainActivity.this.setProgress(newProgress * 1000);
     }
 }

Applog.logString is nothing it's log.d so change it this is working for me very well
于 2013-10-10T11:57:11.850 回答