UriMatcher

作者: 迷途之中小书童 | 来源:发表于2018-08-25 14:01 被阅读0次

使用场景,用来匹配URI,通常用来匹配内容提供器中的URI

使用demo

  • 定义URI匹配的code

    private static final int PEOPLE = 1;
    private static final int PEOPLE_ID = 2;
    private static final int PEOPLE_PHONES = 3;
    private static final int PEOPLE_PHONES_ID = 4;
    private static final int PEOPLE_CONTACTMETHODS = 7;
    private static final int PEOPLE_CONTACTMETHODS_ID = 8;
    private static final int DELETED_PEOPLE = 20;
    private static final int PHONES = 9;
    private static final int PHONES_ID = 10;
    private static final int PHONES_FILTER = 14;
    private static final int CONTACTMETHODS = 18;
    private static final int CONTACTMETHODS_ID = 19;
    private static final int CALLS = 11;
    private static final int CALLS_ID = 12;
    private static final int CALLS_FILTER = 15;

  • 初始化静态对象 UriMatcher
    private static final UriMatcher sURIMatcher = new UriMatcher(UriMatcher.NO_MATCH);

  • 初始化uri和匹配类型规则
    *addURI三个入参,分别为String authority(应用包名), String path(操作的表单路径), int code(当调用UriMatch的match(uri)方法后,会返回uri匹配成功后返回的code码,如果没有匹配match方法将返回-1即UriMatcher.NO_MATCH)

    static
    {
    sURIMatcher.addURI("contacts", "people", PEOPLE);
    sURIMatcher.addURI("contacts", "people/#", PEOPLE_ID);
    sURIMatcher.addURI("contacts", "people/#/phones", PEOPLE_PHONES);
    sURIMatcher.addURI("contacts", "people/#/phones/#", PEOPLE_PHONES_ID);
    sURIMatcher.addURI("contacts", "people/#/contact_methods", PEOPLE_CONTACTMETHODS);
    sURIMatcher.addURI("contacts", "people/#/contact_methods/#", PEOPLE_CONTACTMETHODS_ID);
    sURIMatcher.addURI("contacts", "deleted_people", DELETED_PEOPLE);
    sURIMatcher.addURI("contacts", "phones", PHONES);
    sURIMatcher.addURI("contacts", "phones/filter/", PHONES_FILTER);
    sURIMatcher.addURI("contacts", "phones/#", PHONES_ID);
    sURIMatcher.addURI("contacts", "contact_methods", CONTACTMETHODS);
    sURIMatcher.addURI("contacts", "contact_methods/#", CONTACTMETHODS_ID);
    sURIMatcher.addURI("call_log", "calls", CALLS);
    sURIMatcher.addURI("call_log", "calls/filter/
    ", CALLS_FILTER);
    sURIMatcher.addURI("call_log", "calls/#", CALLS_ID);
    }

    • 这里需要注意,在sdk版本18开始,path参数支持以'/' 开头,例如
sURIMatcher.addURI("contacts", "/people", PEOPLE);
image.png

相关文章

  • UriMatcher

    使用场景,用来匹配URI,通常用来匹配内容提供器中的URI 使用demo 定义URI匹配的codeprivate ...

  • Uri and UriMatcher

    Uri : 通用资源标志符(Universal Resource Identifier, 简称"URI")。 U...

  • Android应用开发:网络编程2

    网络编程 Java基础:网络编程 Uri、URL、UriMatcher、ContentUris详解 Android...

  • Http协议

    网络编程 Java基础:网络编程 Uri、URL、UriMatcher、ContentUris详解 Android...

  • ContentResolver: UriMatcher实现匹配内

    UriMatcher该类 实现内容匹配时共享信息 .addURI(authority,path,自定义代码):接受...

  • ContentProvider

    构建content URI 构建URiMatcher 为多行和单行数据定义final整型常量 为完整的UriMat...

网友评论

    本文标题:UriMatcher

    本文链接:https://www.haomeiwen.com/subject/buxgiftx.html