我正在努力应对<data> http://developer.android.com/guide/topics/manifest/data-element.html中的元素AndroidManifest.xml
文件来让我的 URI 匹配工作。我想匹配以下 URI:
- http://example.com/something http://example.com/something
- http://example.com/foo http://example.com/foo
- http://example.com/foo/ http://example.com/foo/
but not
- http://example.com http://example.com
- http://example.com/ http://example.com/
- http://example.com/something/else http://example.com/something/else
我主要是和它一起工作
<data android:scheme="http"
android:host="example.com"
android:pathPattern="/..*" />
<data android:pathPattern="/..*/" />
但它仍然匹配http://example.com/something/else
.
我怎样才能排除这些?
不幸的是,可用于 pathPattern 标记的通配符非常有限,并且目前通过纯 xml 无法实现您想要的功能。
这是因为一旦你接受"/.*"
一切都被接受(包括斜杠)。由于我们无法提供不被接受的数据标签,唯一的方法就是检查您的活动中的数据。以下是如何完成您要做的事情:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Uri data = getIntent().getData();
Log.d("URI", "Received data: " + data);
String path = data.getPath();
// Match only path "/*" with an optional "/" in the end.
// * to skip forward, backward slashes and spaces
Pattern pattern = Pattern.compile("^/[^\\\\/\\s]+/?$");
Matcher matcher = pattern.matcher(path);
if (!matcher.find()) {
Log.e("URI", "Incorrect data received!");
finish();
return;
}
// After the check we can show the content and do normal stuff
setContentView(R.layout.activity_main);
// Do something when received path data is OK
}
清单内的活动如下所示:
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="http"
android:host="example.com"
android:pathPattern="/.*"/>
</intent-filter>
</activity>
如果您不希望您的活动检查数据是否正确,您将不得不更改您的要求。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)