2016-11-18 60 views
2

我正在嘗試爲我公司的Web應用程序製作應用程序,並且它運行良好。唯一的問題是我有一些來自不同領域的鏈接,比如Youtube,它會打開一些視頻。與指定爲我的域的域相關的所有其他鏈接都將在Web視圖中打開,並在我的應用中顯示該網站。但Youtube鏈接和其他外部網站鏈接不起作用。Webview並未打開Chrome中不同域名的鏈接Android

如果我回想起來,它應該詢問用戶是否要使用基於鏈接的Chrome或Youtube或Facebook應用程序打開它,但它不起作用。

這裏是我的WebView代碼:

public class MainActivity extends Activity { 

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

     final WebView myWebView = (WebView) findViewById(R.id.webview); 
     myWebView.loadUrl("http://www.example.com/"); 

     WebSettings webSettings = myWebView.getSettings(); 
     webSettings.setJavaScriptEnabled(true); 
     myWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true); 
     myWebView.getSettings().setSupportMultipleWindows(true); 
     myWebView.setHorizontalScrollBarEnabled(true); 
     myWebView.getSettings().setDomStorageEnabled(true); // This will allow access 

     myWebView.setWebViewClient(new WebViewClient() { 
      @Override 
      public boolean shouldOverrideUrlLoading(WebView view, String url) { 
       if (Uri.parse(url).getHost().startsWith("google.com")) { 
        view.loadUrl(url); 
       } else { 

        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); 
        startActivity(intent); 

       } 
       return true; 
      } 
     }); 
     myWebView.setDownloadListener(new DownloadListener() { 

      public void onDownloadStart(String url, String userAgent, 
             String contentDisposition, String mimetype, 
             long contentLength) { 
       DownloadManager.Request request = new DownloadManager.Request(
         Uri.parse(url)); 
       final String filename = URLUtil.guessFileName(url, contentDisposition, mimetype); 
       request.allowScanningByMediaScanner(); 
       request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); //Notify client once download is completed! 
       request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, filename); 
       DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE); 
       dm.enqueue(request); 
       Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); //This is important! 
       intent.addCategory(Intent.CATEGORY_OPENABLE); //CATEGORY.OPENABLE 
       intent.setType("*/*");//any application,any extension 
       Toast.makeText(getApplicationContext(), "Downloading File", //To notify the Client that the file is being downloaded 
         Toast.LENGTH_LONG).show(); 

      } 
     }); 
    } 

    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
     // Check if the key event was the Back button and if there's history 
     WebView myWebView = (WebView) findViewById(R.id.webview); 
     if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) { 
      myWebView.goBack(); 
      return true; 
     } 
     // If it wasn't the Back key or there's no web page history, bubble up to the default 
     // system behavior (probably exit the activity) 
     return super.onKeyDown(keyCode, event); 
    } 
} 

我已經試過一切,但它確實從Web瀏覽器不同的域不開的聯繫。哪裏不對?

回答

1

也許你可以在shouldOverrideUrlLoading方法中試試這個來檢查它的公司域是否應該以這種方式在webView中打開。它爲我工作。

public boolean shouldOverrideUrlLoading(WebView webView, String url) { 
    if(url.indexOf("yourcompanydomain.com") > -1) return false; 

    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); 
    activity.startActivity(intent); 
    return true; 
} 

,或者嘗試

if(myWebView.getUrl().startsWith("yourcompanydomain.com")){ 
       return false ; 
} 
else 
{ 
Intent.... 
} 
+0

它說無法解析符號活動。我不知道要在這裏打什麼活動。 –

+0

只是讓它this.startActivity(intent) – user5434084

+0

這對我工作,謝謝! – JoeYo