sat*_*ine 4 android intentfilter android-intent
我的 android 应用程序有一个像这样的意图过滤器:
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<action android:name="android.intent.action.SENDTO" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="satur9nine" android:host="*" />
<data android:scheme="http" android:host="www.satur9nine.com" android:pathPrefix="/app" />
</intent-filter>
Run Code Online (Sandbox Code Playgroud)
它应该匹配 satur9nine://anything 或http://www.satur9nine.com/app/anything。但是它匹配http://www.notmywebsite.com/app,有什么问题?
关于此的文档相当模糊,但您可以通过查看IntentFilter文档中的方法来弄清楚addDataScheme,addDataPath并且addDataAuthority它们都是相互独立的,并且无法将方案、路径和权限添加到一起。
查看IntentFilter 源代码证实了这一点。数据 URI 的每个部分(架构、路径、权限)都存储在自己的 List 中,因此<data>当匹配代码运行时,来自不同元素的值最终会混合在一起,而不是<data>单独检查每个元素。这意味着数据 URI 可以将任何方案与具有任何路径前缀的任何主机相匹配,这不是我们想要的。
解决方案是有多个intent-filter部分,如下所示:
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<action android:name="android.intent.action.SENDTO" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="http" android:host="www.satur9nine.com" android:pathPrefix="/app" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<action android:name="android.intent.action.SENDTO" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="satur9nine" android:host="*" />
</intent-filter>
Run Code Online (Sandbox Code Playgroud)
意图过滤器匹配将以这种方式运行两次,并且不会混合方案、主机和路径。
| 归档时间: |
|
| 查看次数: |
324 次 |
| 最近记录: |