博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android.content.UriMatcher
阅读量:6243 次
发布时间:2019-06-22

本文共 3560 字,大约阅读时间需要 11 分钟。

文档已经讲得好明白了:

Utility class to aid in matching URIs in content providers.

To use this class, build up a tree of UriMatcher objects. For example:

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;    private static final UriMatcher sURIMatcher = new UriMatcher(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);    }

Then when you need to match against a URI, call , providing the URL that you have been given. You can use the result to build a query, return a type, insert or delete a row, or whatever you need, without duplicating all of the if-else logic that you would otherwise need. For example:

public String getType(Uri url)    {        int match = sURIMatcher.match(url);        switch (match)        {            case PEOPLE:                return "vnd.android.cursor.dir/person";            case PEOPLE_ID:                return "vnd.android.cursor.item/person";... snip ...                return "vnd.android.cursor.dir/snail-mail";            case PEOPLE_ADDRESS_ID:                return "vnd.android.cursor.item/snail-mail";            default:                return null;        }    }

instead of:

public String getType(Uri url)    {        List pathSegments = url.getPathSegments();        if (pathSegments.size() >= 2) {            if ("people".equals(pathSegments.get(1))) {                if (pathSegments.size() == 2) {                    return "vnd.android.cursor.dir/person";                } else if (pathSegments.size() == 3) {                    return "vnd.android.cursor.item/person";... snip ...                    return "vnd.android.cursor.dir/snail-mail";                } else if (pathSegments.size() == 3) {                    return "vnd.android.cursor.item/snail-mail";                }            }        }        return null;    }

 

   本文转自老Zhan博客园博客,原文链接:http://www.cnblogs.com/mybkn/archive/2012/05/05/2484580.html,如需转载请自行联系原作者
你可能感兴趣的文章
MYSQL主从不同步延迟原理分析及解决方案
查看>>
使用LeakTracer检测android NDK C/C++代码中的memory leak
查看>>
软件即服务或将使本地Linux应用开发停速
查看>>
Python的学习笔记16------urllib
查看>>
深度剖析安卓Framebuffer设备驱动
查看>>
C/C++那些事儿之 数的转换
查看>>
用ViewPager实现欢迎引导页面
查看>>
ffmpeg源码分析 (三)
查看>>
Oracle11g x64使用Oracle SQL Developer报错:Unable to...
查看>>
概率论与数理统计14--方差
查看>>
关于PHP中按位取反问题
查看>>
scrapy爬取某网站,模拟登陆过程中遇到的那些坑
查看>>
设计师的知识管理
查看>>
Struts中ActionForm的作用
查看>>
昨天开始学习安卓
查看>>
centos 7 chrome安装
查看>>
为什么单个TCP连接很难占满带宽
查看>>
最佳开发实践:自动化单元测试(PHP)
查看>>
postgresql 9.3 install centos6.x
查看>>
Groovy中方法的调用实现方式浅析(CallSite)
查看>>