From the previous article, you have learned how to implement an android custom content provider with examples. This article will show you how to call it’s methods to insert, update, delete and query data from it.
1. Custom Content Provider Invoker Example Overview.
- There are five buttons in this example, when you click each button, it will use ContentResolver to call a custom content provider’s related method to execute user account add, update, delete and query actions.
- All the data is stored in the custom content provider’s SQLite database file Account.db, this file is saved in /data/data/com.dev2qa.example/databases folder in android emulator.
- If you want to see the account data in the SQLite database, you can run the below shell command to see them.
adb root adb shell generic_x86:/ # sqlite3 /data/data/com.dev2qa.example/databases/Account.db SQLite version 3.9.2 2015-11-02 18:31:45 Enter ".help" for usage hints. sqlite> .table account android_metadata sqlite> .schema account CREATE TABLE account( _id integer primary key autoincrement, user_name text, password text, email text, title text ); sqlite> select * from account; 1|Jerry|12345678|jerry@dev2qa.com|CEO
2. Custom Content Provider Consumer App Source Code.
- Below is the example android project files structure.
./ ├── app │ ├── build.gradle │ ├── proguard-rules.pro │ └── src │ ├── main │ │ ├── AndroidManifest.xml │ │ ├── java │ │ │ └── com │ │ │ └── dev2qa │ │ │ └── example │ │ │ ├── datasharing │ │ │ │ └── custom │ │ │ │ ├── CustomContentProviderActivity.java │ │ ├── res │ │ │ ├── layout │ │ │ │ ├── activity_custom_content_provider.xml
2.1 Main Activity Java File.
- CustomContentProviderActivity.java
package com.dev2qa.example.datasharing.custom; import android.content.ContentResolver; import android.content.ContentValues; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.text.TextUtils; import android.view.View; import android.widget.Button; import android.widget.Toast; import com.dev2qa.example.R; import com.dev2qa.example.datasharing.custom.provider.AccountContentProvider; public class CustomContentProviderActivity extends AppCompatActivity { private String newAccountId = ""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_custom_content_provider); setTitle("dev2qa.com - Custom Content Provider Example."); // Add user account through custom content provider. Button addAccountBtn = (Button)findViewById(R.id.custom_content_provider_add_button); addAccountBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // First get contentResolver object. ContentResolver contentResolver = getContentResolver(); // Create account content provider uri object. Uri uri = Uri.parse(AccountContentProvider.BASE_CONTENT_URI); // Put content values. ContentValues contentValues = new ContentValues(); contentValues.put("user_name", "Jerry"); contentValues.put("password", "12345678"); contentValues.put("email", "jerry@dev2qa.com"); contentValues.put("title", "CEO"); // Call contentResolver's insert method to add account. Uri newAccountUri = contentResolver.insert(uri, contentValues); // Get newly created account id. newAccountId = newAccountUri.getPathSegments().get(1); // Show the new account id in a toast popup message. Toast.makeText(getApplicationContext(), "New account id is " + newAccountId, Toast.LENGTH_LONG).show(); } }); // Update user account through custom content provider. Button updateAccountBtn = (Button)findViewById(R.id.custom_content_provider_update_button); updateAccountBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // First get contentResolver object. ContentResolver contentResolver = getContentResolver(); // Create account content provider uri object. Uri uri = Uri.parse(AccountContentProvider.BASE_CONTENT_URI); // Put update values. ContentValues contentValues = new ContentValues(); contentValues.put("password", "!@#$%^&*"); contentValues.put("email", "jerry@gmail.com"); contentValues.put("title", "CFO"); String whereClause = "user_name = ?"; String whereClauseArgs[] = {"Jerry"}; // Call contentResolver's update method to update account info. int updateCount = contentResolver.update(uri, contentValues, whereClause, whereClauseArgs); // Show update account in a toast popup message. Toast.makeText(getApplicationContext(), updateCount + " account has been updated.", Toast.LENGTH_LONG).show(); } }); // Delete user account through custom content provider. Button deleteAccountBtn = (Button)findViewById(R.id.custom_content_provider_delete_button); deleteAccountBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // First get contentResolver object. ContentResolver contentResolver = getContentResolver(); // Create account content provider uri object. Uri uri = Uri.parse(AccountContentProvider.BASE_CONTENT_URI); // Delete condition. String whereClause = "user_name = ?"; String whereClauseArgs[] = {"Jerry"}; // Call contentResolver's delete method to delete account info. int deleteCount = contentResolver.delete(uri, whereClause, whereClauseArgs); // Show delete account in a toast popup message. Toast.makeText(getApplicationContext(), deleteCount + " account has been deleted.", Toast.LENGTH_LONG).show(); } }); // Query new add user account through custom content provider. Button queryAccountBtn = (Button)findViewById(R.id.custom_content_provider_query_button); queryAccountBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // First get contentResolver object. ContentResolver contentResolver = getContentResolver(); // Create account content provider uri object. Uri uri = Uri.parse(AccountContentProvider.BASE_CONTENT_URI); String whereClause = null; // If newAccountId is not empty. if(!TextUtils.isEmpty(newAccountId)) { whereClause = "_id = " + newAccountId; } // Call contentResolver's query method to get account info. Cursor cursor = contentResolver.query(uri, null, whereClause, null, null); StringBuffer msgBuf = new StringBuffer(); if(cursor!=null && cursor.getCount() > 0) { cursor.moveToFirst(); // Get each column info long id = cursor.getLong(cursor.getColumnIndex("_id")); String userName = cursor.getString(cursor.getColumnIndex("user_name")); String password = cursor.getString(cursor.getColumnIndex("password")); String email = cursor.getString(cursor.getColumnIndex("email")); String title = cursor.getString(cursor.getColumnIndex("title")); msgBuf.append("id = "); msgBuf.append(id); msgBuf.append(" , user_name = "); msgBuf.append(userName); msgBuf.append(" , password = "); msgBuf.append(password); msgBuf.append(" , email = "); msgBuf.append(email); msgBuf.append(" , title = "); msgBuf.append(title); }else { msgBuf.append("No account exist."); } // Show account column info in a toast popup message. Toast.makeText(getApplicationContext(), msgBuf.toString(), Toast.LENGTH_LONG).show(); } }); // Query all user account through custom content provider. Button queryAllAccountBtn = (Button)findViewById(R.id.custom_content_provider_query_all_button); queryAllAccountBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // First get contentResolver object. ContentResolver contentResolver = getContentResolver(); // Create account content provider uri object. Uri uri = Uri.parse(AccountContentProvider.BASE_CONTENT_URI); StringBuffer msgBuf = new StringBuffer(); // Call contentResolver's query method to get all account info. Cursor cursor = contentResolver.query(uri, null, null, null, null); if(cursor!=null && cursor.getCount() > 0) { cursor.moveToFirst(); do{ // First empty msg buffer object. msgBuf.delete(0, msgBuf.length()); // Get each column info long id = cursor.getLong(cursor.getColumnIndex("_id")); String userName = cursor.getString(cursor.getColumnIndex("user_name")); String password = cursor.getString(cursor.getColumnIndex("password")); String email = cursor.getString(cursor.getColumnIndex("email")); String title = cursor.getString(cursor.getColumnIndex("title")); msgBuf.append("id = "); msgBuf.append(id); msgBuf.append(" , user_name = "); msgBuf.append(userName); msgBuf.append(" , password = "); msgBuf.append(password); msgBuf.append(" , email = "); msgBuf.append(email); msgBuf.append(" , title = "); msgBuf.append(title); // Show account column info in a toast popup message. Toast.makeText(getApplicationContext(), msgBuf.toString(), Toast.LENGTH_LONG).show(); }while (cursor.moveToNext()); }else { msgBuf.append("No account exist."); // Show account column info in a toast popup message. Toast.makeText(getApplicationContext(), msgBuf.toString(), Toast.LENGTH_LONG).show(); } } }); } }
2.2 Layout XML File.
- activity_custom_content_provider.xml
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <Button android:id="@+id/custom_content_provider_add_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Add account"/> <Button android:id="@+id/custom_content_provider_update_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Update account"/> <Button android:id="@+id/custom_content_provider_delete_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Delete account"/> <Button android:id="@+id/custom_content_provider_query_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Query New Add account"/> <Button android:id="@+id/custom_content_provider_query_all_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Query All account"/> </LinearLayout>
2.3 Android Manifest XML File.
- The provider settings are the custom content provider class which is implemented in example How To Create Android Custom Content Provider Example.
- AndroidManifest.xml
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".datasharing.custom.CustomContentProviderActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <provider android:name=".datasharing.custom.provider.AccountContentProvider" android:authorities="com.dev2qa.account.provider" android:enabled="true" android:exported="true"></provider> </application>