如何在 Android 4.3 中匹配内容 URI 的根?

Posted

技术标签:

【中文标题】如何在 Android 4.3 中匹配内容 URI 的根?【英文标题】:How do I match the root of a content URI in Android 4.3? 【发布时间】:2013-09-19 05:17:18 【问题描述】:

当有人点击我的 ContentProvider 的根目录时,我想返回一些结果,到目前为止它运行良好。但是,从 android 4.3 开始,我无法匹配根!这是我尝试过的所有方法,但没有任何效果。这在 4.3 下返回 -1,但在早期版本下不返回。

如何匹配该 URI?

private int testMatch()
    UriMatcher mUriMatcher = new UriMatcher(0);
    mUriMatcher.addURI("com.someone.app.provider.thingy", "/#", 0);
    mUriMatcher.addURI("com.someone.app.provider.thingy", "/", 1);
    mUriMatcher.addURI("com.someone.app.provider.thingy", "", 2);
    mUriMatcher.addURI("com.someone.app.provider.thingy", "/*", 3);
    mUriMatcher.addURI("com.someone.app.provider.thingy", "#", 4);
    mUriMatcher.addURI("com.someone.app.provider.thingy", "*", 5);
    mUriMatcher.addURI("com.someone.app.provider", "thingy", 6);

    Uri uri=Uri.parse("content://com.someone.app.provider.thingy");

    return mUriMatcher.match(uri);

【问题讨论】:

【参考方案1】:

根据文档,传递给 UriMatcher 构造函数的值用于匹配根路径,但在您的代码中,相同的值 (0) 被路径 /# 覆盖。

试试下面的代码,看看能不能匹配到根路径:

UriMatcher mUriMatcher = new UriMatcher(0);
Uri uri=Uri.parse("content://com.someone.app.provider.thingy");
return mUriMatcher.match(uri);

之所以在 4.3 之前有效,可能是因为 4.3 接受了领先的/

【讨论】:

以上是关于如何在 Android 4.3 中匹配内容 URI 的根?的主要内容,如果未能解决你的问题,请参考以下文章