UriMatcher uri选择器

   选择器是为了让其他应用把传递进来的uri进行分类,来确定要操作哪个表,

   首先要声明UriMatcher

 static UriMatcher um = new UriMatcher(UriMatcher.NO_MATCH);参数固定

然后在静态块里进行匹配规则的说明

// 在块里确定匹配规则,
// 第一个参数为主机名,之后是表名,之后是结果码,一般1开始
um.addURI("com.hello.demo", "people", 1);
um.addURI("com.hello.demo", "teacher", 2);

//路径后带数字匹配
um.addURI("com.hello.demo", "people/#", 3);
//路径后带字符的匹配
um.addURI("com.hello.demo", "people/*", 4);

然后在增啥改查里开始对uri进行分类

@Override
public Uri insert(Uri uri, ContentValues values) {
//uri选择器进行选择,返回结果码
switch (um.match(uri)) {
case 1:
db.insert("people", null, values);
break;
case 2:
db.insert("teacher", null, values);
break;

default:
break;
}

return uri;
}

然后其他应用程序要通过uri调用的时候必须加上匹配规则,否则返回值为-1就匹配不到了

findViewById(R.id.insert_btn).setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
ContentResolver cr = getContentResolver();
ContentValues values = new ContentValues();
values.put("name", "tom");
values.put("sex", "m");
cr.insert(Uri.parse("content://com.hello.demo/people"), values);

}
});
findViewById(R.id.delete_btn).setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
ContentResolver cr = getContentResolver();
ContentValues values = new ContentValues();
values.put("name", "jim");
cr.insert(Uri.parse("content://com.hello.demo/teacher"), values);

}
});

package com.neusoft.demo;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;

public class MyContentProvider extends ContentProvider {
    SQLiteOpenHelper oh;
    SQLiteDatabase db;
     // uri选择器,如果这个数据库中有超过两个表的时候我们就要用uri选择器来确定别的
    // 应用程序要操作的是哪个数据库
    static UriMatcher um = new UriMatcher(UriMatcher.NO_MATCH);
    static {
        // 在块里确定匹配规则,
        // 第一个参数为主机名,之后是表情,之后是结果码,一般1开始
        um.addURI("com.hello.demo", "people", 1);
        um.addURI("com.hello.demo", "teacher", 2);
      

      //路径后带数字匹配
      um.addURI("com.hello.demo", "people/#", 3);
      //路径后带字符的匹配
      um.addURI("com.hello.demo", "people/*", 4);


    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        return db.delete("people", selection, selectionArgs);
    }

    @Override
    public String getType(Uri uri) {
        // TODO: Implement this to handle requests for the MIME type of the data
        // at the given URI.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        //uri选择器进行选择,返回结果码
        switch (um.match(uri)) {
        case 1:
            db.insert("people", null, values);
            break;
        case 2:
            db.insert("teacher", null, values);
            break;

        default:
            break;
        }

        return uri;
    }

    @Override
    public boolean onCreate() {
        oh = new MyOpenHelp(getContext());
        db = oh.getWritableDatabase();
        return false;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
            String[] selectionArgs, String sortOrder) {
        Cursor cu = db.query("people", projection, selection, selectionArgs,
                null, null, sortOrder);
        return cu;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection,
            String[] selectionArgs) {
        return db.update("people", values, selection, selectionArgs);
    }
}

其他应用程序调用

package com.example.contentprovider2;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.insert_btn).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                ContentResolver cr = getContentResolver();
                ContentValues values = new ContentValues();
                values.put("name", "tom");
                values.put("sex", "m");
                cr.insert(Uri.parse("content://com.hello.demo/people"), values);
                

            }
        });
        findViewById(R.id.delete_btn).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                ContentResolver cr = getContentResolver();
                ContentValues values = new ContentValues();
                values.put("name", "jim");
                cr.insert(Uri.parse("content://com.hello.demo/teacher"), values);

            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}
原文地址:https://www.cnblogs.com/84126858jmz/p/4922106.html