

{"id":75452,"date":"2020-02-06T10:22:45","date_gmt":"2020-02-06T04:52:45","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=75452"},"modified":"2021-08-25T13:55:51","modified_gmt":"2021-08-25T08:25:51","slug":"content-provider-in-android","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/content-provider-in-android\/","title":{"rendered":"Content Providers in Android &#8211; Learn to implement various methods"},"content":{"rendered":"<div class='__iawmlf-post-loop-links' style='display:none;' data-iawmlf-post-links='[{&quot;id&quot;:1195,&quot;href&quot;:&quot;https:\\\/\\\/developer.android.com\\\/reference\\\/java\\\/net\\\/URI&quot;,&quot;archived_href&quot;:&quot;http:\\\/\\\/web-wp.archive.org\\\/web\\\/20251010072120\\\/https:\\\/\\\/developer.android.com\\\/reference\\\/java\\\/net\\\/URI&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2025-12-09 02:59:22&quot;,&quot;http_code&quot;:503},{&quot;date&quot;:&quot;2025-12-12 12:44:10&quot;,&quot;http_code&quot;:503},{&quot;date&quot;:&quot;2025-12-16 11:25:40&quot;,&quot;http_code&quot;:503},{&quot;date&quot;:&quot;2025-12-24 17:53:53&quot;,&quot;http_code&quot;:503},{&quot;date&quot;:&quot;2026-01-06 04:01:54&quot;,&quot;http_code&quot;:503},{&quot;date&quot;:&quot;2026-01-09 09:43:19&quot;,&quot;http_code&quot;:503},{&quot;date&quot;:&quot;2026-01-14 11:35:55&quot;,&quot;http_code&quot;:503},{&quot;date&quot;:&quot;2026-01-22 09:32:25&quot;,&quot;http_code&quot;:503},{&quot;date&quot;:&quot;2026-01-28 07:13:23&quot;,&quot;http_code&quot;:503},{&quot;date&quot;:&quot;2026-02-03 15:31:48&quot;,&quot;http_code&quot;:503},{&quot;date&quot;:&quot;2026-02-07 02:56:58&quot;,&quot;http_code&quot;:503},{&quot;date&quot;:&quot;2026-02-11 06:46:31&quot;,&quot;http_code&quot;:503},{&quot;date&quot;:&quot;2026-02-16 07:12:00&quot;,&quot;http_code&quot;:503},{&quot;date&quot;:&quot;2026-02-23 19:04:13&quot;,&quot;http_code&quot;:503},{&quot;date&quot;:&quot;2026-03-02 02:38:58&quot;,&quot;http_code&quot;:503},{&quot;date&quot;:&quot;2026-03-05 14:27:18&quot;,&quot;http_code&quot;:503},{&quot;date&quot;:&quot;2026-03-08 15:14:25&quot;,&quot;http_code&quot;:503},{&quot;date&quot;:&quot;2026-03-12 10:33:03&quot;,&quot;http_code&quot;:503},{&quot;date&quot;:&quot;2026-03-17 06:53:03&quot;,&quot;http_code&quot;:503},{&quot;date&quot;:&quot;2026-03-23 03:53:25&quot;,&quot;http_code&quot;:503},{&quot;date&quot;:&quot;2026-04-03 12:17:12&quot;,&quot;http_code&quot;:503},{&quot;date&quot;:&quot;2026-04-09 01:36:39&quot;,&quot;http_code&quot;:503},{&quot;date&quot;:&quot;2026-04-12 17:05:34&quot;,&quot;http_code&quot;:503},{&quot;date&quot;:&quot;2026-04-21 09:40:28&quot;,&quot;http_code&quot;:503},{&quot;date&quot;:&quot;2026-04-24 12:14:00&quot;,&quot;http_code&quot;:503},{&quot;date&quot;:&quot;2026-04-28 01:59:07&quot;,&quot;http_code&quot;:503},{&quot;date&quot;:&quot;2026-05-03 22:16:47&quot;,&quot;http_code&quot;:503},{&quot;date&quot;:&quot;2026-05-12 00:55:26&quot;,&quot;http_code&quot;:503},{&quot;date&quot;:&quot;2026-05-20 02:31:17&quot;,&quot;http_code&quot;:503},{&quot;date&quot;:&quot;2026-06-11 23:27:52&quot;,&quot;http_code&quot;:503}],&quot;broken&quot;:true,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-06-11 23:27:52&quot;,&quot;http_code&quot;:503},&quot;process&quot;:&quot;done&quot;}]'><\/div>\n<p>Content providers provide the content\/data to Android applications from an Android system or other Android applications. Let\u2019s go through the details of the content provider in Android through this article, but first, let&#8217;s take a glance of topics that we will discuss:<\/p>\n<ul>\n<li>What are Content Providers<\/li>\n<li>CRUD operations<\/li>\n<li>Examples<\/li>\n<li>Accessing data with Content provider<\/li>\n<li>Implementing Content Provider<\/li>\n<\/ul>\n<h3>What are Content Providers in Android?<\/h3>\n<p>Content Providers are an important component of Android. They handle the access to the central repository and supply data from one application to another on request. This task of handling is done by methods of <strong>ContentResolver<\/strong> class. So, content providers can store data in various ways such as files, database or over the internet.<\/p>\n<p>Many a time we need to share our data with applications and that\u2019s where it becomes useful. The following diagram depicts how the content provider helps in sharing data with applications from data stores.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/02\/content-provider-in-android.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75509\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/02\/content-provider-in-android.jpg\" alt=\"content provider in android\" width=\"802\" height=\"420\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/02\/content-provider-in-android.jpg 802w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/02\/content-provider-in-android-150x79.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/02\/content-provider-in-android-300x157.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/02\/content-provider-in-android-768x402.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/02\/content-provider-in-android-520x272.jpg 520w\" sizes=\"auto, (max-width: 802px) 100vw, 802px\" \/><\/a><\/p>\n<p>As can be seen, the content provider lets us collect the data centrally and provide them to applications as shown in the above diagram. Content providers act the same as database and also we can query it to add, delete, insert or update the data.<\/p>\n<p>It can be understood that a content provider hides the database details and also, it lets an application share data among other applications. Content providers are not limited to texts, but also contains images and videos as well.<\/p>\n<h3>CRUD Operations<\/h3>\n<p>Content providers provide the following four basic operations. These are also known as CRUD operations, where<\/p>\n<p><strong>C &#8211; Create<\/strong><br \/>\n<strong>R &#8211; Read<\/strong><br \/>\n<strong>U &#8211; Update<\/strong><br \/>\n<strong>D &#8211; Delete<\/strong><\/p>\n<p>The above are the four operations of content providers :<\/p>\n<ul>\n<li><strong>Create:<\/strong> It is used for the creation of data in content providers.<\/li>\n<li><strong>Read:<\/strong> It reads the data stored in the content provider.<\/li>\n<li><strong>Update:<\/strong> It lets the editing in existing data in content providers.<\/li>\n<li><strong>Delete:<\/strong> It deletes the existing data stored in its Storage.<\/li>\n<\/ul>\n<h3>Examples of Android Content Providers<\/h3>\n<p>To understand content providers with examples, consider the following examples :<\/p>\n<ul>\n<li>The Gallery that contains images.<\/li>\n<li>Contact lists that contain Contact details.<\/li>\n<li>A dictionary that has collections of all the words that are used.<\/li>\n<li>The music playlist has a list of songs.<\/li>\n<li>Call logs contain the call details.<\/li>\n<\/ul>\n<p>This is how exactly content providers work.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/02\/working-of-android-content-provider.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75504\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/02\/working-of-android-content-provider.jpg\" alt=\"working of android content provider\" width=\"648\" height=\"414\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/02\/working-of-android-content-provider.jpg 648w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/02\/working-of-android-content-provider-150x96.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/02\/working-of-android-content-provider-300x192.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/02\/working-of-android-content-provider-520x332.jpg 520w\" sizes=\"auto, (max-width: 648px) 100vw, 648px\" \/><\/a><\/p>\n<h3>Accessing Data with Content Provider<\/h3>\n<p>We need to use the <strong>ContentResolver<\/strong> object in our application, in order to communicate with the content providers for data access. Now to enable communication between the user interface and ContentResolver, we use another object, <strong>CursorLoader<\/strong> to run query asynchronously. This CursorLoader will be called using Activity\/Fragment of the application.<\/p>\n<p>Then, the content provider receives the query from the client and executes and returns the result.<\/p>\n<p>To understand how it works, consider the following diagram:<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/02\/accessing-data-with-android-content-provider.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75505\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/02\/accessing-data-with-android-content-provider.jpg\" alt=\"accessing data with android content provider\" width=\"385\" height=\"684\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/02\/accessing-data-with-android-content-provider.jpg 385w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/02\/accessing-data-with-android-content-provider-84x150.jpg 84w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/02\/accessing-data-with-android-content-provider-169x300.jpg 169w\" sizes=\"auto, (max-width: 385px) 100vw, 385px\" \/><\/a><\/p>\n<p><em><strong>Wait! Have you completed the <a href=\"https:\/\/data-flair.training\/blogs\/android-activity\/\">Android Activity Tutorial<\/a><\/strong><\/em><\/p>\n<h3>Methods of Content Provider in Android<\/h3>\n<p>Let\u2019s see the following methods of content provider:<\/p>\n<ul>\n<li><strong>onCreate() &#8211;<\/strong> This method in Android initializes the provider as soon as the receiver is created.<\/li>\n<li><strong>query() &#8211;<\/strong> It receives a request in the form of a query from the user and responds with a cursor in Android.<\/li>\n<li><strong>insert() &#8211;<\/strong> This method is used to insert the data into our content provider.<\/li>\n<li><strong>update() &#8211;<\/strong> This method is used to update existing data in a row and return the updated row data.<\/li>\n<li><strong>delete() &#8211;<\/strong> This method deletes existing data from the content provider.<\/li>\n<li><strong>getType() &#8211;<\/strong> It returns the Multipurpose Internet Mail Extension type of data to the given Content URI.<\/li>\n<\/ul>\n<h3>Content URIs<\/h3>\n<p>Content URIs are the uniform resource identifiers that identify the data in the content providers. A content URI includes two things:\u00a0<strong>Authority<\/strong> that is the symbolic name of the Provider and a <strong>Path<\/strong> that is a name that points towards the data. Every content provider methods have an argument which is <a href=\"https:\/\/developer.android.com\/reference\/java\/net\/URI\">URI<\/a>.<\/p>\n<p>Let&#8217;s understand the Content Provider URI:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">content:\/\/&lt;authority&gt;\/&lt;path&gt;\/&lt;optional_id&gt;<\/pre>\n<ul>\n<li><strong>content:\/\/ &#8211;<\/strong>\u00a0It\u2019s always present, and is the scheme portion of the URI.<\/li>\n<li><strong>authority &#8211;<\/strong>\u00a0It is the unique name of the content provider, like photos, contacts. It\u2019s a string that can identify the whole content provider.<\/li>\n<li><strong>path &#8211;<\/strong>\u00a0It is often used to identify some or the other data of the provider. The path is mostly used to identify individual tables.<\/li>\n<li><strong>optional_id &#8211;<\/strong>\u00a0id is used to access a single particular record of a file. We use this only in cases where we need to access only a particular record and not the complete file. It\u2019s a numeric identifier to access a particular row of the data table.<\/li>\n<\/ul>\n<p>Content provider also has two wildcards which are:<\/p>\n<ol>\n<li><strong>* &#8211;<\/strong>\u00a0Asterisk (*) is used to match valid characters irrespective of their length.<\/li>\n<li><strong>#\u00a0&#8211;<\/strong> Hash (#) is used to match valid numerals irrespective of their length.<\/li>\n<\/ol>\n<h2>Implementation of Content Provider in Android<\/h2>\n<p>We\u2019ll create a file named ContentProvider and within that, we\u2019ll create a Java file of name <strong>DataFlairProvider.java<\/strong>. Open the new DataFlairProvider.java and write the following code:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">package com.dataflair.contentprovider;\r\nimport android.content.ContentProvider;\r\nimport android.content.ContentUris;\r\nimport android.content.ContentValues;\r\nimport android.content.Context;\r\nimport android.content.UriMatcher;\r\nimport android.database.Cursor;\r\nimport android.database.sqlite.SQLiteDatabase;\r\nimport android.database.sqlite.SQLiteException;\r\nimport android.database.sqlite.SQLiteOpenHelper;\r\nimport android.database.sqlite.SQLiteQueryBuilder;\r\nimport android.net.Uri;\r\nimport java.util.HashMap;\r\n\r\npublic class DataFlairProvider extends ContentProvider {\r\n   static final String PROVIDER_NAME = \"com.dataflair.contentprovider.UserProvider\";\r\n   static final String URL = \"content:\/\/\" + PROVIDER_NAME + \"\/users\";\r\n   static final Uri CONTENT_URI = Uri.parse(URL);\r\n\r\n   static final String id = \"id\";\r\n   static final String name = \"name\";\r\n   static final int uriCode = 1;\r\n   static final UriMatcher uriMatcher;\r\n   private static HashMap&lt;String, String&gt; values;\r\n   static {\r\n      uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);\r\n      uriMatcher.addURI(PROVIDER_NAME, \"users\", uriCode);\r\n      uriMatcher.addURI(PROVIDER_NAME, \"users\/*\", uriCode);\r\n   }\r\n\r\n   @Override\r\n   public String getType(Uri uri) {\r\n       switch (uriMatcher.match(uri)) {\r\n           case uriCode:\r\n               return \"vnd.android.cursor.dir\/users\";\r\n           default:\r\n               throw new IllegalArgumentException(\"Unsupported URI: \" + uri);\r\n       }\r\n   }\r\n\r\n   @Override\r\n   public boolean onCreate() {\r\n       Context context = getContext();\r\n       DatabaseHelper dbHelper = new DatabaseHelper(context);\r\n       db = dbHelper.getWritableDatabase();\r\n       if (db != null) {\r\n           return true;\r\n       }\r\n       return false;\r\n   }\r\n\r\n   @Override\r\n   public Cursor query(Uri uri, String[] projection, String selection,\r\n                       String[] selectionArgs, String sortOrder) {\r\n       SQLiteQueryBuilder qb = new SQLiteQueryBuilder();\r\n       qb.setTables(TABLE_NAME);\r\n\r\n       switch (uriMatcher.match(uri)) {\r\n           case uriCode:\r\n               qb.setProjectionMap(values);\r\n               break;\r\n           default:\r\n               throw new IllegalArgumentException(\"Unknown URI \" + uri);\r\n       }\r\n       if (sortOrder == null || sortOrder == \"\") {\r\n           sortOrder = id;\r\n       }\r\n       Cursor c = qb.query(db, projection, selection, selectionArgs, null,\r\n               null, sortOrder);\r\n       c.setNotificationUri(getContext().getContentResolver(), uri);\r\n       return c;\r\n   }\r\n   @Override\r\n   public Uri insert(Uri uri, ContentValues values) {\r\n       long rowID = db.insert(TABLE_NAME, \"\", values);\r\n       if (rowID &gt; 0) {\r\n           Uri _uri = ContentUris.withAppendedId(CONTENT_URI, rowID);\r\n           getContext().getContentResolver().notifyChange(_uri, null);\r\n           return _uri;\r\n       }\r\n       throw new SQLiteException(\"Failed to add a record into \" + uri);\r\n   }\r\n   @Override\r\n   public int update(Uri uri, ContentValues values, String selection,\r\n                     String[] selectionArgs) {\r\n       int count = 0;\r\n       switch (uriMatcher.match(uri)) {\r\n           case uriCode:\r\n               count = db.update(TABLE_NAME, values, selection, selectionArgs);\r\n               break;\r\n           default:\r\n               throw new IllegalArgumentException(\"Unknown URI \" + uri);\r\n       }\r\n       getContext().getContentResolver().notifyChange(uri, null);\r\n       return count;\r\n   }\r\n   @Override\r\n   public int delete(Uri uri, String selection, String[] selectionArgs) {\r\n       int count = 0;\r\n       switch (uriMatcher.match(uri)) {\r\n           case uriCode:\r\n               count = db.delete(TABLE_NAME, selection, selectionArgs);\r\n               break;\r\n           default:\r\n               throw new IllegalArgumentException(\"Unknown URI \" + uri);\r\n       }\r\n       getContext().getContentResolver().notifyChange(uri, null);\r\n       return count;\r\n   }\r\n   private SQLiteDatabase db;\r\n   static final String DATABASE_NAME = \"EmpDB\";\r\n   static final String TABLE_NAME = \"Employees\";\r\n   static final int DATABASE_VERSION = 1;\r\n   static final String CREATE_DB_TABLE = \" CREATE TABLE \" + TABLE_NAME\r\n           + \" (id INTEGER PRIMARY KEY AUTOINCREMENT, \"\r\n           + \" name TEXT NOT NULL);\";\r\n\r\n   private static class DatabaseHelper extends SQLiteOpenHelper {\r\n       DatabaseHelper(Context context) {\r\n           super(context, DATABASE_NAME, null, DATABASE_VERSION);\r\n       }\r\n\r\n       @Override\r\n       public void onCreate(SQLiteDatabase db) {\r\n           db.execSQL(CREATE_DB_TABLE);\r\n       }\r\n\r\n       @Override\r\n       public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {\r\n           db.execSQL(\"DROP TABLE IF EXISTS \" + TABLE_NAME);\r\n           onCreate(db);\r\n       }\r\n   }\r\n}<\/pre>\n<p>Now, write the following code in <strong>MainActivity.java:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">package com.dataflair.contentprovider;\r\nimport androidx.appcompat.app.AppCompatActivity;\r\nimport android.content.ContentValues;\r\nimport android.content.Context;\r\nimport android.database.Cursor;\r\nimport android.net.Uri;\r\nimport android.os.Bundle;\r\nimport android.view.MotionEvent;\r\nimport android.view.View;\r\nimport android.view.inputmethod.InputMethodManager;\r\nimport android.widget.EditText;\r\nimport android.widget.TextView;\r\nimport android.widget.Toast;\r\n\r\npublic class MainActivity extends AppCompatActivity {\r\n\r\n   @Override\r\n   protected void onCreate(Bundle savedInstanceState) {\r\n       super.onCreate(savedInstanceState);\r\n       setContentView(R.layout.activity_main);\r\n   }\r\n   @Override\r\n   public boolean onTouchEvent(MotionEvent event) {\r\n       InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);\r\n       imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);\r\n       return true;\r\n   }\r\n   public void onClickAddDetails(View view) {\r\n       ContentValues values = new ContentValues();\r\n       values.put(DataFlairProvider.name, ((EditText)        findViewById(R.id.txtName)).getText().toString());\r\n       getContentResolver().insert(DataFlairProvider.CONTENT_URI, values);\r\n       Toast.makeText(getBaseContext(), \"New Record Inserted\", Toast.LENGTH_LONG).show();\r\n   }\r\n\r\n   public void onClickShowDetails(View view) {\r\n\r\n       TextView resultView= (TextView) findViewById(R.id.res);\r\n       Cursor cursor = getContentResolver().query(Uri.parse(\"content:\/\/com.dataflair.contentprovider.UserProvider\/users\"), null, null, null, null);\r\n       if(cursor.moveToFirst()) {\r\n           StringBuilder strBuild=new StringBuilder();\r\n           while (!cursor.isAfterLast()) {\r\n               strBuild.append(\"\\n\"+cursor.getString(cursor.getColumnIndex(\"id\"))+ \"-\"+ cursor.getString(cursor.getColumnIndex(\"name\")));\r\n               cursor.moveToNext();\r\n           }\r\n           resultView.setText(strBuild);\r\n       }\r\n       else {\r\n           resultView.setText(\"No Records Found\");\r\n       }\r\n   }\r\n}<\/pre>\n<p>After that, open <strong>activity_main.xml<\/strong>, write the following code, here we\u2019ve designed the layout of the application:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\r\n&lt;LinearLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n   android:orientation=\"vertical\" android:layout_width=\"match_parent\"\r\n   android:layout_height=\"match_parent\"&gt;\r\n\r\n   &lt;TextView\r\n       android:id=\"@+id\/textView2\"\r\n       android:layout_width=\"wrap_content\"\r\n       android:layout_height=\"wrap_content\"\r\n       android:text=\"DataFlair \"\r\n       android:textColor=\"#a4c639\"\r\n       android:layout_marginLeft=\"100dp\"\r\n       android:textSize=\"50dp\"\r\n       android:layout_centerHorizontal=\"true\" \/&gt;\r\n\r\n   &lt;TextView\r\n       android:layout_width=\"wrap_content\"\r\n       android:layout_height=\"wrap_content\"\r\n       android:layout_marginLeft=\"50dp\"\r\n       android:layout_marginTop=\"50dp\"\r\n       android:text=\"Data Flair User\" \/&gt;\r\n\r\n   &lt;EditText\r\n       android:id=\"@+id\/txtName\"\r\n       android:layout_width=\"wrap_content\"\r\n       android:layout_height=\"wrap_content\"\r\n       android:layout_marginLeft=\"50dp\"\r\n       android:ems=\"10\" \/&gt;\r\n\r\n   &lt;Button\r\n       android:id=\"@+id\/btnAdd\"\r\n       android:layout_width=\"wrap_content\"\r\n       android:layout_height=\"wrap_content\"\r\n       android:layout_marginLeft=\"50dp\"\r\n       android:onClick=\"onClickAddDetails\"\r\n       android:text=\"Add User\" \/&gt;\r\n\r\n   &lt;Button\r\n       android:id=\"@+id\/btnRetrieve\"\r\n       android:layout_width=\"wrap_content\"\r\n       android:layout_height=\"wrap_content\"\r\n       android:layout_marginLeft=\"50dp\"\r\n       android:onClick=\"onClickShowDetails\"\r\n       android:text=\"Show Users\" \/&gt;\r\n\r\n   &lt;TextView\r\n       android:id=\"@+id\/res\"\r\n       android:layout_width=\"match_parent\"\r\n       android:layout_height=\"wrap_content\"\r\n       android:layout_marginLeft=\"50dp\"\r\n       android:clickable=\"false\"\r\n       android:ems=\"10\" \/&gt;\r\n&lt;\/LinearLayout&gt;<\/pre>\n<p>In <strong>manifest.xml<\/strong> file, write the code given below:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\r\n&lt;manifest xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\" package=\"com.dataflair.contentprovider\"&gt;\r\n   &lt;application\r\n       android:allowBackup=\"true\"\r\n       android:icon=\"@mipmap\/ic_launcher\"\r\n       android:label=\"@string\/app_name\"\r\n       android:roundIcon=\"@mipmap\/ic_launcher_round\"\r\n       android:supportsRtl=\"true\"\r\n       android:theme=\"@style\/AppTheme\"&gt;\r\n       &lt;activity android:name=\"com.dataflair.contentprovider.MainActivity\"&gt;\r\n           &lt;intent-filter&gt;\r\n               &lt;action android:name=\"android.intent.action.MAIN\" \/&gt;\r\n               &lt;category android:name=\"android.intent.category.LAUNCHER\" \/&gt;\r\n           &lt;\/intent-filter&gt;\r\n       &lt;\/activity&gt;\r\n       &lt;provider\r\n           android:authorities=\"com.dataflair.contentprovider.UserProvider\"\r\n           android:name=\"com.dataflair.contentprovider.DataFlairProvider\"&gt;\r\n       &lt;\/provider&gt;\r\n   &lt;\/application&gt;\r\n&lt;\/manifest&gt;<\/pre>\n<p>After this, you\u2019ll find the application like this :<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/02\/App1-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-75511 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/02\/App1-1.png\" alt=\"content provider app\" width=\"300\" height=\"533\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/02\/App1-1.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/02\/App1-1-84x150.png 84w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/02\/App1-1-169x300.png 169w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Now you can enter the data and retrieve it with the help of Content Provider:<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/02\/App2-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-75512 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/02\/App2-1.png\" alt=\"content provider app\" width=\"300\" height=\"533\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/02\/App2-1.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/02\/App2-1-84x150.png 84w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/02\/App2-1-169x300.png 169w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<h2>Summary<\/h2>\n<p>In this article, we\u2019ve learned what is a content provider and how it works. We\u2019ve also seen the implementation of the content provider in Android, I hope you\u2019ll try it out and enjoy learning Android.<\/p>\n<p>Any queries in the Android content provider article? Do mention in the comment section.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Content providers provide the content\/data to Android applications from an Android system or other Android applications. Let\u2019s go through the details of the content provider in Android through this article, but first, let&#8217;s take&#46;&#46;&#46;<\/p>\n","protected":false},"author":10,"featured_media":75509,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18722],"tags":[21781,21821,21823,21820,21822],"class_list":["post-75452","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-android","tag-android-oncreate","tag-android-query","tag-android-update-method","tag-content-provider-in-android","tag-insert-android"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Content Providers in Android - Learn to implement various methods - DataFlair<\/title>\n<meta name=\"description\" content=\"Android Content Provider - Learn about content providers in Android with their examples, methods and implementation. Also, learn to access data with content provider.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/content-provider-in-android\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Content Providers in Android - Learn to implement various methods - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Android Content Provider - Learn about content providers in Android with their examples, methods and implementation. Also, learn to access data with content provider.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/content-provider-in-android\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-02-06T04:52:45+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-08-25T08:25:51+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/02\/content-provider-in-android.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"802\" \/>\n\t<meta property=\"og:image:height\" content=\"420\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"10 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Content Providers in Android - Learn to implement various methods - DataFlair","description":"Android Content Provider - Learn about content providers in Android with their examples, methods and implementation. Also, learn to access data with content provider.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/content-provider-in-android\/","og_locale":"en_US","og_type":"article","og_title":"Content Providers in Android - Learn to implement various methods - DataFlair","og_description":"Android Content Provider - Learn about content providers in Android with their examples, methods and implementation. Also, learn to access data with content provider.","og_url":"https:\/\/data-flair.training\/blogs\/content-provider-in-android\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2020-02-06T04:52:45+00:00","article_modified_time":"2021-08-25T08:25:51+00:00","og_image":[{"width":802,"height":420,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/02\/content-provider-in-android.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"10 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/content-provider-in-android\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/content-provider-in-android\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/a90b082e16aa38d207212d22b0581f33"},"headline":"Content Providers in Android &#8211; Learn to implement various methods","datePublished":"2020-02-06T04:52:45+00:00","dateModified":"2021-08-25T08:25:51+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/content-provider-in-android\/"},"wordCount":947,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/content-provider-in-android\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/02\/content-provider-in-android.jpg","keywords":["android oncreate","android query","android update method","content provider in android","insert android"],"articleSection":["Android Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/content-provider-in-android\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/content-provider-in-android\/","url":"https:\/\/data-flair.training\/blogs\/content-provider-in-android\/","name":"Content Providers in Android - Learn to implement various methods - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/content-provider-in-android\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/content-provider-in-android\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/02\/content-provider-in-android.jpg","datePublished":"2020-02-06T04:52:45+00:00","dateModified":"2021-08-25T08:25:51+00:00","description":"Android Content Provider - Learn about content providers in Android with their examples, methods and implementation. Also, learn to access data with content provider.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/content-provider-in-android\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/content-provider-in-android\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/content-provider-in-android\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/02\/content-provider-in-android.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/02\/content-provider-in-android.jpg","width":802,"height":420,"caption":"content provider in android"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/content-provider-in-android\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Android Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/android\/"},{"@type":"ListItem","position":3,"name":"Content Providers in Android &#8211; Learn to implement various methods"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/a90b082e16aa38d207212d22b0581f33","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/dd6de0d647a0185cd6faf264e4ba860b0d85d08d7070766f9cd41bea5bb0b227?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/dd6de0d647a0185cd6faf264e4ba860b0d85d08d7070766f9cd41bea5bb0b227?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/dd6de0d647a0185cd6faf264e4ba860b0d85d08d7070766f9cd41bea5bb0b227?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"The DataFlair Team is passionate about delivering top-notch tutorials and resources on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. With expertise in the tech industry, we simplify complex topics to help learners excel. Stay updated with our latest insights.","url":"https:\/\/data-flair.training\/blogs\/author\/dfadteam1\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/75452","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=75452"}],"version-history":[{"count":9,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/75452\/revisions"}],"predecessor-version":[{"id":76061,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/75452\/revisions\/76061"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/75509"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=75452"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=75452"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=75452"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}