内容提供者URI匹配器

时间:2011-06-03 11:26:12

标签: android android-contentprovider

在实施ContentProvider时,在文档中有明确的推荐用于定义所有uris。但我对URI匹配器部分感到困惑:例如,我有包org.company.example,表名为'items',然后我定义

 public static final Uri CONTENT_URI = 
  Uri.parse("content://org.company.example.sampleprovider/items");

我应该使用什么权限部分来匹配静态init中的URI:

 private static final UriMatcher uriMatcher;

  static {
   uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
   uriMatcher.addURI("what goes here?", "items", ITEM);
   uriMatcher.addURI("what goes here?", "items/#", ITEM_ID);
  }

1 个答案:

答案 0 :(得分:14)

public static final String PROVIDER_NAME = "org.company.example.sampleprovider";
public static final Uri CONTENT_URI = Uri.parse("content://" + PROVIDER_NAME);
private static final UriMatcher uriMatcher;
static {
   uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
   uriMatcher.addURI(PROVIDER_NAME, "items", ITEM);
   uriMatcher.addURI(PROVIDER_NAME, "items/#", ITEM_ID);
}