Webview不会在Chrome Android中打开不同域的链接(Webview is not opening links of different domains in chrome Androi

编程入门 行业动态 更新时间:2024-10-28 14:24:57
Webview不会在Chrome Android中打开不同域的链接(Webview is not opening links of different domains in chrome Android)

我正在尝试为我公司的Web应用程序制作一个应用程序并且运行良好。 唯一的问题是我有一些来自不同域的链接,比如Youtube,它会打开一些视频。 与指定为我的域的域相关的所有其他链接将在Webview中打开,并在我的应用程序中显示该网站。 但是Youtube链接和其他外部网站链接不起作用。

如果我记得,它应该询问用户是否要使用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浏览器中打开来自不同域的链接。 哪里不对?

I am trying to make an app for my company's web application and it works well. Only problem is that I have some links that are from different domains, like Youtube, which opens some videos. All other links related to the domain specified as my domain will open in the Webview and display the website in my app. But the Youtube link and other external website links don't work.

If I recall, it should ask the user if he wants to open it using Chrome or Youtube or the Facebook app based on the link but it doesn't work.

Here is my WebView code:

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); } }

I have tried everything but it doesn't open links from different domains in the web browser. What is wrong?

最满意答案

也许你可以在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.... }

Maybe you can try this in the shouldOverrideUrlLoading method to check if its company domain, it should open in the webView by this way. It has worked for me.

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; }

or try

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

更多推荐

本文发布于:2023-04-28 09:46:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1330944.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:会在   链接   Android   Chrome   Webview

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!