欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

ContentProvider内容提供者

程序员文章站 2022-07-08 10:42:39
...

ContentProvider内容提供者

ContentProvider内容提供者(四大组件之一)主要用于在不同的应用程序之间实现数据共享的功能

	有内容提供方和内容索要方
	  索要方是contentResolver
	  如果访问系统的自带数据需要加权限
	  url 是统一资源标识符

还有内容提供方 contentProvider
需要继承 ContentProlider
重写六个方法 进行创建数据库 增删改查操作

内容提供者代码

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

    @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");
    }

内容索要方代码


	 ContentResolver resolver = getContentResolver();
        Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
        String[] s = new String[]{ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER};
        Cursor cursor = resolver.query(uri, s, null, null, null);

        while (cursor.moveToNext()) {
            User user = new User();
            user.name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
//            user.phone = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

            Log.e("user####", user.toString());
        }
  ContentResolver resolver = getContentResolver();
        Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
        String[] strings = new String[]{MediaStore.Images.Media.TITLE, MediaStore.Images.Media.SIZE, MediaStore.Images.Media.DATA};
        Cursor cursor = resolver.query(uri, strings, null, null, null);

        while (cursor.moveToNext()) {
            User user = new User();
            user.name = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.TITLE));
//            user.phone=cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.SIZE));
//            user.date=cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));

            Log.e("pic####", user.toString());
        }