UriMatcher是content providers用來比對Uri所使用的
用法很簡單, 就是先給予訓練劇本之後
輸入對應的Uri就會回對應的值

void addURI(String authority, String path, int code)
用來指定Uri以及該回傳的值
int match(Uri uri)
用來判斷Uri, 並傳回對應的值

一個App指需要一個UriMatcher
所以會宣告成static final的形式
而UriMatcher.NO_MATC代表沒匹配時會回傳的值 -1
會用static block內給初值
其中#代表任意數字, *代表任意的字元

private static final UriMatcher sURIMatcher = new UriMatcher(UriMatcher.NO_MATCH);

static {
sURIMatcher.addURI("contacts", "people", 1);
sURIMatcher.addURI("contacts", "people/#", 2);
sURIMatcher.addURI("contacts", "people/#/phones/#", 3);
sURIMatcher.addURI("contacts", "phones/filter/*", 4);
sURIMatcher.addURI("call_log", "calls", 5);
sURIMatcher.addURI("call_log", "calls/#", 6);
}

在使用上則用match的方式即可

sURIMatcher.match(Uri.parse("content://contacts/people"));
sURIMatcher.match(Uri.parse("content://contacts/people/123"));
sURIMatcher.match(Uri.parse("content://contacts/people/1/phones/3"));
sURIMatcher.match(Uri.parse("content://contacts/phones/filter/abc"));
sURIMatcher.match(Uri.parse("content://call_log//calls"));
sURIMatcher.match(Uri.parse("content://call_log//calls/3"));

 

1. 官網https://developer.android.com/reference/android/content/UriMatcher.html

01.png

 

2. 給予設定值

02.png

 

3. 用match來判斷

03.png

 

4. 結果

04.png  

arrow
arrow
    文章標籤
    Android UriMatcher
    全站熱搜

    RX1226 發表在 痞客邦 留言(0) 人氣()