To access data from a content provider, URI is used as a query string.
[content://authority/optionalPath/optionalID]
// defining authority so that other application can access it
static final String PROVIDER_NAME = "com.demo.user.provider";
// defining content URI
static final String URL = "content://" + PROVIDER_NAME + "/users";
// parsing the content URI
static final Uri CONTENT_URI = Uri.parse(URL);
Implement query()
, insert()
, update()
, delete()
, update()
, onCreate()
methods and operate the database.
// class to add values in the database
ContentValues values = new ContentValues();
// fetching text from user
values.put(MyContentProvider.name, ((EditText) findViewById(R.id.textName)).getText().toString());
// inserting into database through content URI
getContentResolver().insert(MyContentProvider.CONTENT_URI, values);
// creating a cursor object of the content URI
Cursor cursor = getContentResolver().query(Uri.parse("content://com.demo.user.provider/users"), null, null, null, null);
// iteration of the cursor to print whole table
if(cursor.moveToFirst()) {
StringBuilder strBuild=new StringBuilder();
while (!cursor.isAfterLast()) {
strBuild.append("\n"+cursor.getString(cursor.getColumnIndex("id"))+ "-"+ cursor.getString(cursor.getColumnIndex("name")));
cursor.moveToNext();
}
resultView.setText(strBuild);
Add certain URI and put && retrieve data same as in same application.
Uri CONTENT_URI = Uri.parse("content://com.demo.user.provider/users");