安卓四大组件——ContentProvider

1.作用

管理 结构化数据集的访问 ,封装数据,应用间数据共享

2.生命周期

只是它属于系统应用、所以随系统启动而初始化,随系统关机而结束;

3.具体实现

自定义的ContentProvider

有六个需要重写的方法,分别是创建和增删改查,

                                        还有一个getType()方法,这个方法用来返回uri对象所对应的MIME类型指定对应的uri地址,然后这写uri地址做逻辑判断,等到其他程序来调用的时候,通过这些逻辑判断来进行安全控制和访问内容的限制

public class AccessProvider extends ContentProvider {
    public AccessProvider() {
    }

    private static UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
    static {
        uriMatcher.addURI("com.example.contentProvider.provider1",
                "table1", 1);
        uriMatcher.addURI("com.example.contentProvider.provider1",
                "table1/#", 2);
        uriMatcher.addURI("com.example.contentProvider.provider1",
                "table2", 3);
        uriMatcher.addURI("com.example.contentProvider.provider1",
                "table2/#", 4);
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        // Implement this to handle requests to delete one or more rows.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @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) {
        // TODO: Implement this to handle requests to insert a new row.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public boolean onCreate() {
        // TODO: Implement this to initialize your content provider on startup.
        return false;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
                        String[] selectionArgs, String sortOrder) {
        switch (uriMatcher.match(uri)){
            case 1:
                Log.d("AccessProvider", "进入了第一个判定条件");break;
            case 2:
                Log.d("AccessProvider", "进入了第二个判定条件");break;
            case 3:
                Log.d("AccessProvider", "进入了第三个判定条件");break;
            case 4:
                Log.d("AccessProvider", "进入了第四个判定条件");break;
            default:
                break;
        }
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @RequiresApi(api = Build.VERSION_CODES.O)


    @Override
    public int update(Uri uri, ContentValues values, String selection,
                      String[] selectionArgs) {
        // TODO: Implement this to handle requests to update one or more rows.
        throw new UnsupportedOperationException("Not yet implemented");
    }
}

上一篇:STM32学习和实践笔记(7):改变系统时钟的实验


下一篇:q @ k运算及att = (q @ k.transpose(-2, -1))含义