【问题标题】:How to force open URL in WebView? Set App as Default for WebView URL如何在 WebView 中强制打开 URL?将应用程序设置为 WebView URL 的默认值
【发布时间】:2016-05-28 06:25:47
【问题描述】:

我正在尝试从浏览器和 Google 搜索中的 URL 打开我的 WebView 应用程序。

我尝试使用此处建议的以下代码:How to launch app on click of url in android

    <intent-filter>
        <data android:scheme="https" />
        <data android:scheme="https" android:host="www.webviewurl.com"/>
        <action android:name="android.intent.action.VIEW"/>
        <category android:name="android.intent.category.BROWSABLE"/>
        <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>

但它没有用。

我想强制打开所有网址,例如:

https://*.webviewurl.com/*
http://webviewurl.com/*

到 WebView 中的https://www.webviewurl.com/*

我已经在服务器中使用.htaccess 完成了这部分。

我希望 URL 默认在 WebView 中打开。

【问题讨论】:

    标签: android android-studio webview


    【解决方案1】:

    您可以使用WebViewClient 来执行此任务。

     mWebView = (WebView) findViewById(R.id.activity_main_webview);
    
        progressBar = (ProgressBar) findViewById(R.id.progressBar1);
    
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        mWebView.loadUrl("http://chatadda.com");
    
        mWebView.setWebViewClient(new WebViewClient(){
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
    
            String url2="http://www.chatadda.com/";
             // all links  with in ur site will be open inside the webview 
             //links that start ur domain example(http://www.chatadda.com/)
            if (url != null && url.startsWith(url2)){
                return false;
                } 
           // all links that points outside the site will be open in a normal android browser
          else  {
                view.getContext().startActivity(
                new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
                return true;
                }
        }
    });
    

    【讨论】:

    • 你能发布完整的 MainActivity.java 代码吗?
    • 你导入了吗? import android.content.Intent;
    • 在 Activity 顶部写下其他导入的行。
    • 工作但应用的高度变为几个像素?
    • 查看您的布局并进行适当的更改,如果有效,请接受已解决的答案。
    【解决方案2】:

    通过执行以下操作已解决该问题:

    AndroidManifest.xml

                <intent-filter>
                    <data android:scheme="https" />
                    <data android:scheme="https" android:host="www.webviewurl.com"/>
                    <action android:name="android.intent.action.VIEW"/>
                    <category android:name="android.intent.category.BROWSABLE"/>
                    <category android:name="android.intent.category.DEFAULT"/>
                </intent-filter>
    

    MainActivity.java

    public class webclient extends WebViewClient {
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if(url.contains("play.google")){
                    view.getContext().startActivity(
                            new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
                }
                if ((url.contains("webviewurl.com"))) {
                    view.loadUrl(url);
                    return true;
                } else {
                    view.getContext().startActivity(
                            new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
                }
                return true;
            }
    

    哪个工作正常。唯一的问题是,所有页面都在应用程序中打开主页。

    就像用户从浏览器点击链接说https://www.website.com/folder/page,它会在应用程序中打开https://www.website.com/

    我该如何解决这个问题?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-22
      • 1970-01-01
      • 2017-02-20
      • 1970-01-01
      • 2016-11-30
      相关资源
      最近更新 更多