top of page

ANDROID CONTENT PROVIDER - Android App Assignment Help

WHAT IS CONTENT PROVIDER IN ANDROID? Content provide is a part of application which is responsible for sharing data between different applications. one application send request to another application such requests are handled by methods of ContentResolver class. After receiving the result targeted applications sends the data to the requester application. we can read values from the data with the help of cursor.


PRACTICAL INTEGRATION OF CONTENT PROVIDER

In this section we are going to build an app for fetching the all contacts information from contacts application of device to our app.


Prerequisites :

Android studio must be installed in the system

Basic knowledge of cursor

Basic knowledge of SQLite queries

Basic knowledge of Android development


IMPLEMENTATION:

Open Android studio can click on create new project, name it Contacts Reader provide package name and create it.




After successful project creation open activity_main.xml file and create a button in center of screen, we write logic of fetching details in the onClick listener of this button


So our activity_main.xml should look something like this:


<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="get contacts"
        android:textSize="16sp"
        android:background="@color/colorPrimary"
        android:textColor="#fff"
        android:padding="10dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Let's move on to the MainActivity.java file here we will write the main program. Initialize the button and call getContacts(); method inside onClick listener.


we will call content provider and query the phonebook database for getting contacts list but before getting the list of contacts we first need the read contacts permission from user.


Go to manifest file and add the below written permission in it

<uses-permission android:name="android.permission.READ_CONTACTS"/>

Now, we are all ready to write getContacts() method, paste the below written code in your getContacts method


private void getContacts() {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_CONTACTS}, 0);
        }

    ContentResolver resolver = getContentResolver();
    Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
    Cursor cursor = resolver.query(uri, null, null, null);
    if(cursor.getCount()>0){
        while (cursor.moveToNext()){
            String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            Log.d("DEMO","NAME : "+ name+", Number : "+ number);

        }
    }

}

So finally, your MainActivity.java file should look like this :


package com.practice.contactsreader;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.content.ContentResolver;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import java.util.jar.Attributes;

public class MainActivity extends AppCompatActivity {

    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getContacts();
            }
        });
    }

    private void getContacts() {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_CONTACTS}, 0);
        }

    ContentResolver resolver = getContentResolver();
    Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
    Cursor cursor = resolver.query(uri, null, null, null);
    if(cursor.getCount()>0){
        while (cursor.moveToNext()){
            String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            Log.d("DEMO","NAME : "+ name+", Number : "+ number);

        }
    }

}
}

Now, connect your real device with android studio and launch application in it at first click of button application will ask for permission from user and at second click if permission is granted then you will receive the entire list of contacts in logcat. you can now use this list of contacts anywhere and for any purpose.




Important link

For more info on this topic visit this link.

Comments


bottom of page