android-Scheme与网页跳转原生的三种方式

参考:Android业务组件化之URL Scheme使用

什么是 URL Scheme?

android中的scheme是一种页面内跳转协议,是一种非常好的实现机制,通过定义自己的scheme协议,可以非常方便跳转app中的各个页面;通过scheme协议,服务器可以定制化告诉App跳转那个页面,可以通过通知栏消息定制化跳转页面,可以通过H5页面跳转页面等。

URL Scheme应用场景:

客户端应用可以向操作系统注册一个 URL scheme,该 scheme 用于从浏览器或其他应用中启动本应用。通过指定的 URL 字段,可以让应用在被调起后直接打开某些特定页面,比如商品详情页、活动详情页等等。也可以执行某些指定动作,如完成支付等。也可以在应用内通过 html 页来直接调用显示 app 内的某个页面。综上URL Scheme使用场景大致分以下几种:

服务器下发跳转路径,客户端根据服务器下发跳转路径跳转相应的页面
H5页面点击锚点,根据锚点具体跳转路径APP端跳转具体的页面
APP端收到服务器端下发的PUSH通知栏消息,根据消息的点击跳转路径跳转相关页面
APP根据URL跳转到另外一个APP指定页面

URL Scheme协议格式:

String urlStr="http://www.orangecpp.com:80/tucao?id=hello&name=lily";
//url =            protocol + authority(host + port) + path + query
//协议protocol=    http
//域名authority=   www.orangecpp.com:80
//页面path=          /tucao
//参数query=       id=hello&name=lily

//authority =      host + port
//主机host=        www.orangecpp.com
//端口port=        80

URL Scheme如何使用:

1.)设置Scheme

在AndroidManifest.xml中对标签增加设置Scheme

 <activity
            android:name=".GoodsDetailActivity"
            android:theme="@style/AppTheme">
            <!--要想在别的App上能成功调起App,必须添加intent过滤器-->
            <intent-filter>
                <!--协议部分,随便设置 
                    xl://goods:8888/goodsDetail?goodsId=10011002
                 -->
                <data android:scheme="xl" 
                        android:host="goods"  
                        android:path="/goodsDetail" 
                        android:port="8888"/> 

                <!--下面这几行也必须得设置-->
                <category android:name="android.intent.category.DEFAULT"/>
                <action android:name="android.intent.action.VIEW"/>
                <category android:name="android.intent.category.BROWSABLE"/>
            </intent-filter>
</activity>

2.)获取Scheme跳转的参数

Uri uri = getIntent().getData();
if (uri != null) {
    // 完整的url信息
    String url = uri.toString();
    Log.e(TAG, "url: " + uri);
    // scheme部分
    String scheme = uri.getScheme();
    Log.e(TAG, "scheme: " + scheme);
    // host部分
    String host = uri.getHost();
    Log.e(TAG, "host: " + host);
    //port部分
    int port = uri.getPort();
    Log.e(TAG, "host: " + port);
    // 访问路劲
    String path = uri.getPath();
    Log.e(TAG, "path: " + path);
    List<String> pathSegments = uri.getPathSegments();
    // Query部分
    String query = uri.getQuery();
    Log.e(TAG, "query: " + query);
    //获取指定参数值
    String goodsId = uri.getQueryParameter("goodsId");
    Log.e(TAG, "goodsId: " + goodsId);
}

3.)调用方式

网页上

<a href="xl://goods:8888/goodsDetail?goodsId=10011002">打开商品详情</a>

原生调用

Intent intent = new Intent(Intent.ACTION_VIEW, 
        Uri.parse("xl://goods:8888/goodsDetail?goodsId=10011002"));
startActivity(intent);

4.)如何判断一个Scheme是否有效

PackageManager packageManager = getPackageManager();
Intent intent = new Intent(Intent.ACTION_VIEW,  
        Uri.parse("xl://goods:8888/goodsDetail?goodsId=10011002"));
List<ResolveInfo> activities = packageManager.queryIntentActivities(intent, 0);
boolean isValid = !activities.isEmpty();
if (isValid) {
    startActivity(intent);
}

webview跳转到activity 示例:

demo项目地址

参考:

Android:你要的WebView与 JS 交互方式 都在这里了
Android WebView加载本地html并实现Java与JS交互
WebView 链接(或按钮)向 Activity 跳转的几种实现方式

本地网页(放在assets目录下):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-CN" dir="ltr">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<div>
    <a href="TOPICID://aa.bb:80/test?p=12&d=1">重定向和scheme跳转button</a>
</div>

<div>
    <input type="button" value="js与android交互button" onClick="javascript:android.toActivity()"/>
</div>
</body>
</html>

参考:Android:你要的WebView与 JS 交互方式 都在这里了

方式一:shouldOverrideUrlLoading重定向

    <WebView
        android:id="@+id/myWebView1"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />
        /*shouldOverrideUrlLoading重定向 跳转*/
        WebView myWebView1 = (WebView) findViewById(R.id.myWebView1);
        myWebView1.loadUrl("file:///android_asset/test1.html");//加载本地的url
        myWebView1.setWebViewClient(new myWebViewClient());


    private class myWebViewClient extends WebViewClient {
        @SuppressWarnings("deprecation")
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) { 
            /*此处获取的url的scheme都是小写*/
            if (!TextUtils.isEmpty(url) && url.contains("topicid://aa.bb:80/test?p=12&d=1")) {
                Intent intent = new Intent();
                intent.setClass(WebViewActivity.this, MainActivity.class);
                startActivity(intent);
            }
            return true;
        }
    }

在Android通过WebViewClient复写shouldOverrideUrlLoading ():
优点:不存在通过 WebView的addJavascriptInterface()进行对象映射 的漏洞;
缺点:JS获取Android方法的返回值复杂。
如果JS想要得到Android方法的返回值,只能通过 WebView 的 loadUrl ()去执行 JS 方法把返回值传递回去

方式二:scheme 跳转

    <WebView
        android:id="@+id/myWebView2"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_marginTop="20dp"
        android:layout_weight="1" />
         /*scheme 跳转*/
        //scheme跳转,不可重写shouldOverrideUrlLoading方法,否则scheme不起作用
        WebView myWebView2 = (WebView) findViewById(R.id.myWebView2);
        myWebView2.loadUrl("file:///android_asset/test1.html");

需要配置intent-filter,不可以重写shouldOverrideUrlLoading

        <!--跳转后的页面-->
        <activity
            android:name="com.example.webviewtest.MainActivity"
            android:label="@string/app_name">

            <intent-filter>
                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />

                <!--TOPICID://aa.bb:80/test?p=12&d=1-->
                <!--scheme类似http和https,注意要转换为小写-->
                <data
                    android:host="aa.bb"
                    android:path="/test"
                    android:port="80"
                    android:scheme="topicid" />
            </intent-filter>
        </activity>

方式三:js与android交互 跳转

    <WebView
        android:id="@+id/myWebView3"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_marginTop="20dp"
        android:layout_weight="1" />
        /*js与android交互 跳转*/
        WebView myWebView3 = (WebView) findViewById(R.id.myWebView3);
        myWebView3.loadUrl("file:///android_asset/test1.html");
        myWebView3.getSettings().setJavaScriptEnabled(true);
        // 与js交互,JsCallAndroidinterface 是个接口,与js交互时用到的, 
        //这个接口实现了从网页跳到app中的activity 的方法,特别重要
        myWebView3.addJavascriptInterface(new JsCallAndroidinterface(this), "android");

    private class JsCallAndroidinterface {

        Activity mActivity;

        JsCallAndroidinterface(Activity mActivity) {
            this.mActivity = mActivity;
        }

        /**
         * 与js交互时用到的方法,在js里直接调用的
         * 定义JS需要调用的方法,被JS调用的方法必须加入@JavascriptInterface注解, 
         * 否则HTML中的js报错:toActivity不是一个function
         */
        @JavascriptInterface
        public void toActivity() {
            System.out.println("----------------toActivity--");
            mActivity.startActivity(new Intent(mActivity, MainActivity.class));
        }
    }

通过 WebView的addJavascriptInterface()进行对象映射:
优点:使用简单 , 仅将Android对象和JS对象映射即可
缺点:存在严重的漏洞问题,具体请看文章:
你不知道的 Android WebView 使用漏洞

  • 5
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值