2016-11-18 1 views
2

私の会社のウェブアプリケーション用のアプリケーションを作成しようとしていて、うまく機能しています。問題はYouTubeにいくつかの動画を開くような異なるドメインのリンクがあることだけです。私のドメインとして指定されたドメインに関連する他のすべてのリンクはWebviewで開き、私のアプリにウェブサイトを表示します。しかし、YouTubeのリンクや他の外部ウェブサイトのリンクは機能しません。Webviewがクロムの異なるドメインのリンクを開こうとしていません

私が思い出したように、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(インテント) – user5434084

+0

これは私のために働いた、ありがとう! – JoeYo

関連する問題