一、URL Scheme 的作用
Scheme 用于从浏览器或其他应用中启动本应用。也就是说要从其他应用中跳转本应用的界面或者网页跳转本应用打开特定的界面。
二、 在 Android 应用中配置 Scheme
1、 只有一个 Scheme 的情况下
在 AndroidManifest.xml 中定义 intent-filter,代码实例:
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:launchMode="singleTask"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<!-- 在主 Activity 中配置 scheme-->
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
<data
android:host="test"//
android:scheme="http//" />//自取名字,可随意命名,提前订好协议
</intent-filter>
</activity>
这个时候通过 URL http//test就可以把应用调起来了。
2、两个或者两个以上的 Scheme 添加
如果我们的应用需要配置多个 Scheme,可以配置多个 intent-filter,代码示例:
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:launchMode="singleTask"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<!-- 在主 Activity 中配置 scheme-->
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
<data
android:host="debugmode"
android:scheme="http//" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
<data
android:host="test"
android:scheme="http//" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
<data
android:host="test2"
android:scheme="https" />
</intent-filter>
</activity>
也有人说在同一个 intent-filter 标签里面配置多个 data 标签也是可以实现功能的。
一般情况下是没有什么问题的,虽然同一过滤器可以包含多个 <data>
元素,但如果您想要声明唯一网址(例如特定的 scheme
和 host
组合),请务必创建单独的过滤器,因为同一 intent 中的多个 <data>
元素实际上会合并在一起以涵盖合并后属性的所有变体。请看看以下代码示例:
<intent-filter>
...
<data android:scheme="http" android:host="test" />
<data android:scheme="https" android:host="test2" />
</intent-filter>
intent 过滤器似乎仅支持 http://test
和 https:/test2
。
其实,还有另外两项:http://test2
和 https://test
- END