WeatherProvider.java 文件源码

java
阅读 36 收藏 0 点赞 0 评论 0

项目:ubiquitous 作者:
/**
 * Creates the UriMatcher that will match each URI to the CODE_WEATHER and
 * CODE_WEATHER_WITH_DATE constants defined above.
 * <p>
 * It's possible you might be thinking, "Why create a UriMatcher when you can use regular
 * expressions instead? After all, we really just need to match some patterns, and we can
 * use regular expressions to do that right?" Because you're not crazy, that's why.
 * <p>
 * UriMatcher does all the hard work for you. You just have to tell it which code to match
 * with which URI, and it does the rest automagically. Remember, the best programmers try
 * to never reinvent the wheel. If there is a solution for a problem that exists and has
 * been tested and proven, you should almost always use it unless there is a compelling
 * reason not to.
 *
 * @return A UriMatcher that correctly matches the constants for CODE_WEATHER and CODE_WEATHER_WITH_DATE
 */
public static UriMatcher buildUriMatcher() {

    /*
     * All paths added to the UriMatcher have a corresponding code to return when a match is
     * found. The code passed into the constructor of UriMatcher here represents the code to
     * return for the root URI. It's common to use NO_MATCH as the code for this case.
     */
    final UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
    final String authority = WeatherContract.CONTENT_AUTHORITY;

    /*
     * For each type of URI you want to add, create a corresponding code. Preferably, these are
     * constant fields in your class so that you can use them throughout the class and you no
     * they aren't going to change. In Sunshine, we use CODE_WEATHER or CODE_WEATHER_WITH_DATE.
     */

    /* This URI is content://com.example.android.sunshine/weather/ */
    matcher.addURI(authority, WeatherContract.PATH_WEATHER, CODE_WEATHER);

    /*
     * This URI would look something like content://com.example.android.sunshine/weather/1472214172
     * The "/#" signifies to the UriMatcher that if PATH_WEATHER is followed by ANY number,
     * that it should return the CODE_WEATHER_WITH_DATE code
     */
    matcher.addURI(authority, WeatherContract.PATH_WEATHER + "/#", CODE_WEATHER_WITH_DATE);

    return matcher;
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号