其他分享
首页 > 其他分享> > Android 中Scheme协议的使用详解唤起Activity或App

Android 中Scheme协议的使用详解唤起Activity或App

作者:互联网

1. 什么是URL Scheme?

是一种页面内跳转协议;通过定义自己的scheme协议,可以非常方便跳转app中的各个页面。

2.什么时候使用

  1. 服务器下发跳转路径,客户端根据服务器下发跳转路径跳转相应的页面
  2. APP根据URL跳转到另外一个APP指定页面。
  3. H5页面点击描点,根据描点具体跳转路径APP端跳转具体的页面
  4. 根据通知也可以跳转到指定页面

3.协议格式

例:myscheme://myhost:8888/macthDetail?macthId=222&name=hello

scheme协议名称myscheme必填
host代表Schema作用于哪个地址域myhost必填
port代表该路径的端口号88888不必填
path代表Schema指定的页面/macthDetail不必填
-代表传递的参数?macthId=222&name=hello不必填

4.在app中如何使用

 <activity android:name=".SecondActivity">
            <intent-filter>
            	//三个action必填
                <action android:name="android.intent.action.VIEW"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="android.intent.category.BROWSABLE"/>
                <data android:scheme="myscheme"
                    android:host="myhost"
                    android:port="8888"
                    android:path="/macthDetail"/>
            </intent-filter>
        </activity>

5.如何调用

1.在html中调用非常简单

<a href="myscheme://myhost:8888/macthDetail?macthId=222&name=hello">打开源生应用指定的页面</a>

2.在源生应用中调用也很简单

Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse("myscheme://myhost:8888/macthDetail?macthId=222&name=hello"));
startActivity(intent); 

6.在源生界面获取uri和各个参数

 		Intent intent = getIntent();
        Uri data = intent.getData();  //
        String action = intent.getAction();
        String scheme = intent.getScheme();
        Set<String> categories = intent.getCategories();
        Log.e("TAG", "data==========="+data);
        Log.e("TAG", "action==========="+action);
        Log.e("TAG", "categories==========="+categories);
        Log.e("TAG", "DataString==========="+intent.getDataString());
        Log.e("TAG", "==============================");
        Log.e("TAG", "scheme==========="+scheme);
        Log.e("TAG", "id ==========="+data.getQueryParameterNames());
        Log.e("TAG", "host==========="+data.getHost());
        Log.e("TAG", "path==========="+data.getPath());
        Log.e("TAG", "port==========="+data.getPort());

输出结果

4-11 18:13:56.335 5198-5198/com.phone.myapplication E/TAG: data===========zymobi://3g2win:9999/macthDetail?goodsId=10011002&time=1111
04-11 18:13:56.335 5198-5198/com.phone.myapplication E/TAG: action===========android.intent.action.VIEW
04-11 18:13:56.335 5198-5198/com.phone.myapplication E/TAG: categories===========null
04-11 18:13:56.335 5198-5198/com.phone.myapplication E/TAG: DataString===========zymobi://3g2win:9999/macthDetail?goodsId=10011002&time=1111
04-11 18:13:56.335 5198-5198/com.phone.myapplication E/TAG: ==============================
04-11 18:13:56.335 5198-5198/com.phone.myapplication E/TAG: scheme===========zymobi
04-11 18:13:56.335 5198-5198/com.phone.myapplication E/TAG: id ===========[goodsId, time]
04-11 18:13:56.335 5198-5198/com.phone.myapplication E/TAG: host===========3g2win
04-11 18:13:56.335 5198-5198/com.phone.myapplication E/TAG: path===========/macthDetail
04-11 18:13:56.335 5198-5198/com.phone.myapplication E/TAG: port===========9999

标签:5198,App,TAG,myapplication,56.335,Activity,Android,com,Log
来源: https://blog.csdn.net/qq_34906385/article/details/121344153