h5页面跳转到Android页面

介绍

尝试webview中的h5页面跳转到Android原生页面时,遇到了 net::ERR_UNKNOWN_URL_SCHEME的问题;
而浏览器中的h5页面跳转到Android原生页面则正常,而且还弹出了两个应用程序,可能scheme链接和该应用重复了;

解决办法:因为webview只能识别http和https协议,遇到图中这种"android://"开头的自定义协议时就无法识别,便会提示ERR_UNKNOWN_URL_SCHEME这样的错误。

webview中的h5页面跳转到Android原生页面报错
浏览器中的h5页面跳转到Android原生页面则正常

代码

HTML

该scheme链接是Android配置的,可以传一些参数;

<a href="android://h5/open?type=1&id=7#fragmentTest">点击跳转到安卓</a>

Android

配置AndroidManifest.xml中的activity,我选择的是h5页面跳转到MainActivity配置页面中;所以在该activity中添加的intent-filter配置,并配置好想要的scheme链接;

        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <!--程序入口-->
                <action android:name="android.intent.action.MAIN" />
                <!--常与action android:name="android.intent.action.MAIN"配合使用,用于表名应用的第一个启动的Activity-->
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

            <!--h5跳转app -->
            <!--需要添加下面的intent-filter配置-->
            <intent-filter>
                <!--通过一个应用来显示数据-->
                <action android:name="android.intent.action.VIEW" />
                <!--默认值,没有该默认值则无法响应隐式意图-->
                <category android:name="android.intent.category.DEFAULT" />
                <!--该组件可以通过浏览器打开-->
                <category android:name="android.intent.category.BROWSABLE" />
                <!--android:scheme="android"    用来辨别启动的app-->
                <!--android:host="h5"           可以当成是一个域名,这边建议使用应用的包名-->
                <!--android:pathPrefix="/open"  参数路径前缀-->
                <data android:host="h5" android:pathPrefix="/open" android:scheme="android" />  <!--android://h5/open-->
            </intent-filter>
        </activity>

MainActivity文件,配置好intent和获取scheme链接;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        Intent intent = getIntent();
        Log.d("test123",""+intent);
        String action = intent.getAction();
        if (Intent.ACTION_VIEW.equals(action)) { //判断是否是我们指定的 action
            Uri uri = intent.getData(); //将String类型的URL转变为URI
            if (uri != null) {
                String type = uri.getQueryParameter("type"); //获取参数
                String id = uri.getQueryParameter("id");
                String fragment = uri.getFragment();
                Log.d("test123",""+uri);
                Log.d("test123",""+type);
                Log.d("test123",""+id);
                Log.d("test123",""+fragment);
            }
        }
    }
}

webview中配置scheme链接可正常跳转;
即对shouldOverriderUrlLoading方法中的参数 String url进行判断,url.startsWith(“http://”)||url.startsWith(“https://”) 即表示加载的url是 http/https 协议,不对其进行拦截处理,反之则进行拦截处理。

public class WebViewActivity extends AppCompatActivity {
    private WebView webView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.webview_layout);
        webView = findViewById(R.id.wView);

        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (url.startsWith("http://") || url.startsWith("https://")) { //加载的url是http/https协议地址
                    view.loadUrl(url);
                    return false; //返回false表示此url默认由系统处理,url未加载完成,会继续往下走
                } else { //加载的url是自定义协议地址
                    try {
                        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                        startActivity(intent);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    return true;
                }
            }
        });
        webView.getSettings().setJavaScriptEnabled(true);//是否允许执行js,默认为false。设置true时,会提醒可能造成XSS漏洞
        webView.loadUrl("http://192.168.0.105:8089/test3");          //调用loadUrl方法为WebView加入链接
    }
}

实现效果

webview中的h5页面可成功跳转到Android原生页面
sheme链接相关信息获取的打印

本文参考

实现h5链接打开Android app
解决webview出现错误net::err_unknown_url_scheme 的问题
WebView出现net::ERR_UNKNOWN_URL_SCHEME错误
Uri详解之——Uri结构与代码提取

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值