Android 页面Scheme配置

一、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

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值