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
  • 10
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android app 使用scheme可以让其他应用程序或者网页链接打开你的应用程序,并且可以携带参数,实现应用程序之间的跳转和交互。以下是Android app 使用scheme的步骤: 1. 在AndroidManifest.xml文件注册scheme。例如,你可以在<activity>标签添加一个<intent-filter>标签,并且在<intent-filter>标签添加一个<data>标签,如下所示: ```xml <activity android:name=".MainActivity"> <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:scheme="myapp" android:host="openactivity" /> </intent-filter> </activity> ``` 2. 在应用程序处理scheme。在你的MainActivity,你可以通过以下代码来处理scheme: ```java @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Intent intent = getIntent(); String action = intent.getAction(); Uri data = intent.getData(); if (data != null) { String host = data.getHost(); if (host.equals("openactivity")) { //处理scheme参数,打开指定的Activity } } } ``` 3. 在其他应用程序或者网页使用scheme打开你的应用程序。例如,你可以在浏览器输入“myapp://openactivity”来打开你的应用程序并且跳转到指定的Activity。 注意事项: 1. 必须在<intent-filter>标签添加<category android:name="android.intent.category.BROWSABLE" />,否则无法在浏览器打开应用程序。 2. scheme参数区分大小写。 3. 如果你的应用程序有多个Activity需要处理scheme,需要在<intent-filter>标签分别添加<data>标签,每个<data>标签的scheme和host必须不同。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值