ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java – Webview不会在Chrome Android中打开不同域的链接

2019-07-06 02:25:43  阅读:84  来源: 互联网

标签:java android android-studio android-webview


我正在尝试为我公司的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浏览器中打开来自不同域的链接.怎么了?

解决方法:

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

标签:java,android,android-studio,android-webview
来源: https://codeday.me/bug/20190706/1393229.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有