2013-02-20 257 views
1

我想在webview中打開url,但它在外部瀏覽器中打開url。webview loadUrl在瀏覽器中打開url

我的代碼是

public class MainActivity extends Activity 
{ 

    private WebView webView; 

    String URL = "https://login.salesforce.com/services/oauth2/authorize?response_type=token&display=touch&client_id=3MVG9Y6d_Btp4xp7w.6oGLerlRztTTUKMEL8QWHvuB5miUgdJzQ0HgnMB7dhm1mTiluRv4ud9cZYeFcAz7hXP&redirect_uri=https%3A%2F%2Flogin.salesforce.com%2Fservices%2Foauth2%2Fsuccess"; 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 



     setContentView(R.layout.webview); 

     webView = (WebView) findViewById(R.id.webView1); 
     webView.getSettings().setJavaScriptEnabled(true); 

     webView.getSettings().setDomStorageEnabled(true); 
     webView.loadUrl(URL); 
     //webView.loadUrl("https://www.google.co.in/"); 
     webView.setWebViewClient(new MyWebViewClient()); 
    } 

    private class MyWebViewClient extends WebViewClient { 
     @Override 
     public boolean shouldOverrideUrlLoading(WebView view, String url) { 
      if (Uri.parse(url).getHost().equals(url)) { 
       // This is your web site, so do not override; let the WebView to 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; 
     } 

     @Override 
     public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { 
      super.onReceivedSslError(view, handler, error); 

      // this will ignore the Ssl error and will go forward to your site 
      handler.proceed(); 
     } 
    } 

} 

如果URL爲https:www.google.com那麼URL僅在web視圖打開。我一直在努力尋找解決方案,但我無法找到它。如何解決這個問題?

回答

2

的問題是在你的

public boolean shouldOverrideUrlLoading(WebView view, String url) { 
     if (Uri.parse(url).getHost().equals(url)) { 
      // This is your web site, so do not override; let the WebView to 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; 
    } 

它試圖加載的URL爲不同的意圖,因此您的默認瀏覽器踢英寸 你到底要檢查與

烏里.parse(url).getHost()。equals(url)

嘗試將檢查日誌,你會弄清楚什麼是錯的。

+0

雅這是我的錯誤 – 2013-02-20 12:53:03

+0

thinks buddy!節省了我的時間 – 2017-07-31 23:40:33

相關問題