contendprovider的简单使用

1.写一个类继承于ContentProvider


    private  static UriMatcher  matcher = new UriMatcher(UriMatcher.NO_MATCH);
    static {
        matcher.addURI("com.contentprovider", "xaioer", SUCCESS);
    }
    
    @Override
    public Uri insert(Uri uri, ContentValues values) {
        if (matcher.match(uri)== SUCCESS) {
            System.out.println("成功了");
        }else {
            throw new RuntimeException("出错了;额");
        }
        return uri;
    }

2.xml中定义 authorities用来进行认证

 

3.设置守卫:

private  static UriMatcher  matcher = new UriMatcher(UriMatcher.NO_MATCH);
    static {
        matcher.addURI("com.contentprovider", "xaioer", SUCCESS);
    }

4.验证守卫的code

public Uri insert(Uri uri, ContentValues values) {
        if (matcher.match(uri)== SUCCESS) {
            System.out.println("成功了");
        }else {
            throw new RuntimeException("出错了;额");
        }
        return uri;
    }

5.其他程序进行访问

    ContentResolver contentResolver = getContentResolver();
        
        Uri uri = Uri.parse("content://com.contentprovider/xaioer");
        ContentValues values = new ContentValues();
        contentResolver.insert(uri, values);

你可能感兴趣的:(contendprovider的简单使用)