2011-03-05 16 views
2

、特にこの方法はAndroidのWebViewの

private void setupWebView(){ 
    String MAP_URL = "http://gmaps-samples.googlecode.com/svn/trunk/articles-android-webmap/simple-android-map.html"; 
    String centerURL = "javascript:centerAt(" + mostRecentLocation.getLatitude() + ","+ mostRecentLocation.getLongitude()+ ")"; 
    webView = (WebView) findViewById(R.id.webview); 
    webView.getSettings().setJavaScriptEnabled(true); 
    //Wait for the page to load then send the location information 
    webView.setWebViewClient(new WebViewClient(){ 
     @Override 
     public void onPageFinished(WebView view, String url){ 
      webView.loadUrl(centerURL); 
     } 
    }); 
    webView.loadUrl(MAP_URL); 
} 

私が気づいたこと私が置いた場合、この

private void setupWebView(){ 
    String MAP_URL = "http://gmaps-samples.googlecode.com/svn/trunk/articles-android-webmap/simple-android-map.html"; 
    String centerURL = "javascript:centerAt(" + mostRecentLocation.getLatitude() + "," + mostRecentLocation.getLongitude()+ ")"; 
    webView = (WebView) findViewById(R.id.webview); 
    webView.getSettings().setJavaScriptEnabled(true); 
    //Wait for the page to load then send the location information 
    webView.setWebViewClient(new WebViewClient(){ 
     @Override 
     public void onPageFinished(WebView view, String url){ 
      //DO NOTHING 
     } 
    }); 
    webView.loadUrl(MAP_URL); 
    webView.loadUrl(centerURL); 
} 

それはもはや作品のようなwebView.loadUrl(centerURL);直後 webView.loadUrl(MAP_URL); 。だからcentreAt(..) javascriptメソッドはMAP_URLに含まれています。

URLが実際に読み込まれる前にwebView.loadUrl(..)メソッドが返されているのだろうかと思います。 トップメソッドが完全にロードされてからJavaScriptが実行されるまで待ちますので、

答えて

13

はい、webView.loadUrl()は非同期です。即時に戻り、WebViewは独自のスレッドで動作し続けます。 WebViewのページの読み込み使用WebViewClient.onPageFinished(..)監視するに

webview.setWebViewClient(new WebViewClient() { 
    public void onPageFinished(WebView view, String url) { 
     // do something here 
    } 
}); 
+1

うめき声を....私はちょうどそれを発見するために一日無駄にしました。しかたがない。ピーターを確認するための乾杯。 – Tim