2

アプリに次のコードがあります。

public class Home extends Activity{
@Override 
public void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.home);
    final ProgressDialog progressBar;
    if(isOnline()){
        WebView webView = (WebView) findViewById(R.id.home_web);
        webView.setBackgroundColor(Color.parseColor(getString(R.color.colore_bg)));
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setBuiltInZoomControls(true);
        webView.getSettings().setPluginsEnabled(true);
        webView.setWebViewClient(new MyWebViewClient());
        progressBar = ProgressDialog.show(this,getString(R.string.caricamento),getString(R.string.attendere));
        webView.setWebViewClient(new WebViewClient(){
            public void onPageFinished(WebView view, String url) {
                if (progressBar.isShowing()) {
                    progressBar.dismiss();
                }
            }
        });
        webView.loadUrl("http://www.mysite.com/android.php");
    }else{
        Toast.makeText(this,getString(R.string.no_connessione),Toast.LENGTH_LONG).show();
    }
}
private class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
            System.out.println("here");
        if (Uri.parse(url).getHost().equals("mysite.com")) {
            // This is my web site, so do not override; let my WebView load the page
            return false;
        }
        // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        startActivity(intent);
        return true;
    }
}
public boolean isOnline(){
    ConnectivityManager cm=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo ni = cm.getActiveNetworkInfo();
    if(ni==null){
        return false;
    }
    return ni.isConnected();
}
}

shouldOverrideUrlLoading が機能せず、system.out も出力されず、呼び出されないようです。どうすればこれを修復できますか? すべてのリンク (メイン ページ www.mysite.com/iphone.php を除く) をデフォルトのブラウザーで開く必要があります。

4

1 に答える 1

6

WebViewClientを2回設定したため、最初のWebViewClient(shouldOverrideUrlLoading)を2番目のWebViewClient(onPageFinished)に置き換えました。それが機能するために2つを組み合わせます:

public class Home extends Activity{
private ProgressDialog progressBar;

@Override 
public void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.home);
    if(isOnline()){
        progressBar = ProgressDialog.show(this,getString(R.string.caricamento),getString(R.string.attendere));
        WebView webView = (WebView) findViewById(R.id.home_web);
        webView.setBackgroundColor(Color.parseColor(getString(R.color.colore_bg)));
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setBuiltInZoomControls(true);
        webView.getSettings().setPluginsEnabled(true);
        webView.setWebViewClient(new MyWebViewClient());
        webView.loadUrl("http://www.mysite.com/android.php");
    }else{
        Toast.makeText(this,getString(R.string.no_connessione),Toast.LENGTH_LONG).show();
    }
}

private class MyWebViewClient extends WebViewClient {

    @Override
    public void onPageFinished(WebView view, String url) {
        if (progressBar != null && progressBar.isShowing()) {
            progressBar.dismiss();
        }
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
            System.out.println("here");
        if (Uri.parse(url).getHost().equals("mysite.com")) {
            // This is my web site, so do not override; let my WebView load the page
            return false;
        }
        // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        startActivity(intent);
        return true;
    }
}
}

(悪いフォーマットは無視してください:p)

于 2012-10-25T16:29:42.277 に答える