美文网首页
Aroute之Scheme

Aroute之Scheme

作者: Method | 来源:发表于2021-07-04 11:27 被阅读0次

    是什么

    Scheme是界面跳转协议,类似URL地址,通过url可以打开某个网站,而通过Scheme可以打开app的某个界面。

    客户端应用可以向操作系统注册一个URL Scheme,该scheme用于从浏览器或其他应用中启动本应用,通过scheme协议来跳转到相应的APP界面,比如商品详情,活动详情,商家详情等等界面

    Scheme格式

    [scheme:][//host:port][path][?query][#fragment]

    例如:test://shangjia:8888/shangjiaDetail?shangjiaId=222#watson

    scheme : test
    host : shangjia
    port : 8888
    path : /shangjiaDetail
    query : shangjiaId=222
    fragment : watson
    

    使用

    配置Scheme协议

    <!--服务器下发跳转路径跳转Activity-->
            <activity
                android:name=".GoodsDetailActivity"
                android:theme="@style/AppTheme">
                <intent-filter>
                    <!--协议部分,随便设置-->
                    <data android:scheme="watson" android:host="goods" android:path="/goodsDetail" android:port="8888"/>
                    <!--下面这两项必须得设置-->
                    <category android:name="android.intent.category.DEFAULT"/>
                    <action android:name="android.intent.action.VIEW"/>
                </intent-filter>
            </activity>
    
    
            <!--H5页面点击描点跳转Activity-->
            <activity android:name=".LoginActivity">
                <intent-filter>
                    <!--下面这两项必须得设置-->
                    <action android:name="android.intent.action.VIEW" />
                    <category android:name="android.intent.category.DEFAULT" />
                    <!--如果通过网页中点击链接跳转下面这项也必须得设置-->
                    <category android:name="android.intent.category.BROWSABLE" />
                    <data
                        android:host="login"
                        android:scheme="test" />
    
                     <data
                        android:host="start"
                        android:scheme="test" /> 
                </intent-filter>
            </activity>
    

    当网页或者是Android代码发送这种规则scheme的请求的时候就能够调用起对应的Activity了。

    通过服务器下发跳转路径跳转相应页面

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

    根据uri取匹配注册文件的Activity

    public class GoodsDetailActivity extends Activity{
        public String TAG = "huaxun";
    
        public void onCreate(Bundle b) {
            super.onCreate(b);
            setContentView(R.layout.activity_detail);
            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, "port: " + port);
                // 访问路径
                String path = uri.getPath();
                Log.e(TAG, "path: " + path);
                // Query部分
                String query = uri.getQuery();
                Log.e(TAG, "query: " + query);
                //获取指定参数值
                String goodsId = uri.getQueryParameter("goodsId");
                Log.e(TAG, "goodsId: " + goodsId);
            }
        }
    }
    

    通过H5页面的锚点跳转相应的页面

    <!DOCTYPE html>
    <html>
    <head>
        <title>Js调用Android</title>
    </head>
    
    <body>
    <a href="test://start/?id=431&name=watson&age=29">跳转start</a>
    <a href="test://web/?id=432&name=jack&age=28">跳转web</a>
    <a href="test://login/?id=433&name=tom&age=27">跳转Login</a>
    </body>
    </html>
    

    匹配Scheme有两种方法

    第一种 拦截Url

    web.loadUrl("file:///android_asset/web.html");
    web.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            Log.v("huaxun","view = [" + view + "], url = [" + url + "]");
            Uri uri = Uri.parse(url);
            String id = uri.getQueryParameter("id");
            String name = uri.getQueryParameter("name");
            String age = uri.getQueryParameter("age");
            Log.v("huaxun","id:" + id);
            Log.v("huaxun","name:" + name);
            Log.v("huaxun","age:" + age);
            if (url.startsWith("test://login")) {
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("test://login/userDetail?id="+id+"&name="+name+"&age="+age));
                startActivity(intent);
            }
            return true;
    }
    

    第二种 配置intet-filter

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

    原文链接

    相关文章

      网友评论

          本文标题:Aroute之Scheme

          本文链接:https://www.haomeiwen.com/subject/hycpultx.html