shouldOverrideUrlLoading未检测到html5中内部视图的哈希网址更改

时间:2013-10-10 11:17:52

标签: android webview android-webview

我正在开发web + android项目,但事实是我无法从shouldOverrideUrlLoading获取url。 我认为这是因为嵌套的html视图和动态数据分配到html容器中。 在html web视图中有滑动菜单,就像facebook,gmail类型的滑动菜单,但它在html中。 当你点击该菜单的任何项目我不能得到网址..(我想确定用户点击哪个按钮或菜单项。)

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

****我认为我的网站开发人员加载了一个索引页面,并且他放了一个div标签,当你从一个页面移动到另一个页面时他只是改变了div标签的内容,这就是为什么我没有得到url在shouldOverrideUrlLoading有任何其他方法来检索用户点击按钮或项目****

有没有办法跟踪嵌套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é</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>

Android代码

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

1 个答案:

答案 0 :(得分:-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