ContentProvider和ContentResolver的使用

创建ContentProvider

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

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs)
    {
        System.out.println(uri+"-------delete---------");
        System.out.println("selection参数为:"+selection);
        return 0;
    }

    //该方法的返回值代表了该ContentProvider所提供数据的MIME类型
    @Override
    public String getType(Uri uri)
    {
        return null;
    }

    //实现插入的方法,该方法返回新插入的记录的uri
    @Override
    public Uri insert(Uri uri, ContentValues values)
    {
        System.out.println(uri+"-------insert---------");
        System.out.println("values参数为:"+values);
        return null;
    }

    //第一次创建该contentProvider时调用该方法
    @Override
    public boolean onCreate()
    {
        System.out.println("-------on create---------");
        return true;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
            String[] selectionArgs, String sortOrder)
    {
        System.out.println(uri+"----query---");
        System.out.println("where参数为:"+selection);
        return null;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection,
            String[] selectionArgs)
    {
        System.out.println(uri+"----update---");
        System.out.println("selection参数为:"+selection+",values参数为:"+values);
        return 0;
    }
}

在另一个应用中通过ContentResolver调用

public class MainActivity extends AppCompatActivity
{
    private ContentResolver contentResolver;
    private Uri uri = Uri.parse("content://com.dailei.test/");
    private Cursor cursor;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        contentResolver = getContentResolver();
        Button insert = (Button)findViewById(R.id.insert);
        Button query = (Button)findViewById(R.id.query);
        if (insert != null)
        {
            insert.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    insert();
                }
            });
        }
        if (query != null)
        {
            query.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    query();
                }
            });
        }

    }

    public void query()
    {
        cursor = contentResolver.query(uri,null,"query_where",null,null);
        Toast.makeText(this,"远程ContentProvide的返回的Cursor为:"+cursor,Toast.LENGTH_LONG).show();

    }

    public void insert()
    {
        ContentValues values = new ContentValues();
        values.put("name","dailei");
        Uri newUri = contentResolver.insert(uri,values);
        Toast.makeText(this,"远程ContentProvide新插入的数据为:"+newUri,Toast.LENGTH_LONG).show();
    }

    @Override
    protected void onDestroy()
    {
        super.onDestroy();
        if(cursor != null)
        {
            cursor.close();
        }
    }
}

ContentResolver第一次访问该ContentProvider的时候,onCreate()方法会被回调,onCreate()方法只会被调用一次。ContentProvider提供的query()、insert()、update()和delete()方法则由其他应用通过ContentResolver()调用。


你可能感兴趣的:(Android)