GTU Android Program 12 Read PhoneBook (Contacts)

GTU Android Program 12 Understanding content providers and permissions Read phonebook contacts using content providers and display in list

GTU Android Program 12

Nowadays most of the social apps are using contacts to find who is a friend. Let’s take an example of WhatsApp or hike apps, which are reading all contacts from the phone book and searching on their server about how many are registered and display information to the users. Using this application you can able to read all your contacts.

Question-related with android read contacts and write contacts

  • Define  the permissions your application  needs to register for the following task Read Contacts
  • What permission is needed and how to specify that for the following. To access the private user information provided by the Contacts content provider.
  • What is special permission needed to access the private user information provided by the Contacts content provider? Also, write the code to start reading contact data from Contacts application
  • List permission required for the following task. Read contacts


Are you spending lots of daily time on android development, then read android tips and tricks to save your time.

tep 1 : Create New Android Project.

Step 2 : Add following permission into AndroidManifest.xml file.

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

 

 

Step 3 : check out java source code and xml.

Location : res\layout

FileName: activity_main.xml

<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:tools=”http://schemas.android.com/tools”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:paddingBottom=”@dimen/activity_vertical_margin”
android:paddingLeft=”@dimen/activity_horizontal_margin”
android:paddingRight=”@dimen/activity_horizontal_margin”
android:paddingTop=”@dimen/activity_vertical_margin”
tools:context=”gtu.android.prog12.MainActivity” >

<TextView
android:id=”@+id/textView1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentTop=”true”
android:layout_centerHorizontal=”true”
android:layout_marginTop=”15dp”
android:text=”Read Contacts”
android:textColor=”#0000ff”
android:textAppearance=”?android:attr/textAppearanceLarge” />

<ListView
android:id=”@+id/listView1″
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:layout_above=”@+id/textView2″
android:layout_below=”@+id/textView1″
android:layout_centerHorizontal=”true”
android:layout_marginTop=”34dp” >

</ListView>

<TextView
android:id=”@+id/textView2″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentBottom=”true”
android:layout_centerHorizontal=”true”
android:layout_marginBottom=”15dp”
android:text=”Developed By: Adarsh Patel”
android:textColor=”#0000ff”
android:textAppearance=”?android:attr/textAppearanceLarge” />

</RelativeLayout>

 

Location : src\package\

FileName : MainActivity.java

package gtu.android.prog12;

/*
* GTU Android Program 12 Understanding content providers and permissions Read phonebook contacts using content providers and display in list
*
*/
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class MainActivity extends Activity {

String namecsv=””;
String phonecsv=””;

String namearray[];
String phonearray[];

ListView lv1;

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

//====================================================================
//ListView to Display Contact Names and on click Phone Number in Toast.
//====================================================================
lv1 = (ListView) findViewById(R.id.listView1);

Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
while (phones.moveToNext())
{

//Read Contact Name
String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));

//Read Phone Number
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

if(name!=null)
{
namecsv += name + “,”;
phonecsv += phoneNumber + “,”;
}

}
phones.close();

//==============================================
// Convert csvstrimg into array
//==============================================
namearray = namecsv.split(“,”);
phonearray = phonecsv.split(“,”);

//Create Array Adapter and Pass ArrayOfValues to it.
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,android.R.id.text1,namearray);

//BindAdpater with our Actual ListView
lv1.setAdapter(adapter);

//Do something on click on ListView Click on Items
lv1.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {

//============================================
// Display number of contact on click.
//===========================================
String msg = phonearray[arg2];
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
}
});

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

 

Location : \

FileName : AndroidManifest.java

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”gtu.android.prog12″
android:versionCode=”1″
android:versionName=”1.0″ >

<uses-sdk
android:minSdkVersion=”15″
android:targetSdkVersion=”15″ />
<uses-permission android:name=”android.permission.READ_CONTACTS”/>

<application
android:allowBackup=”true”
android:icon=”@drawable/ic_launcher”
android:label=”@string/app_name”
android:theme=”@style/AppTheme” >
<activity
android:name=”.MainActivity”
android:label=”@string/app_name” >
<intent-filter>
<action android:name=”android.intent.action.MAIN” />

<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
</application>

</manifest>

 

 

Output 1

GTU Android Program 12

 

Download Complete Project with All Files

Download Few Popular Android Apps

  1. Bank IFSC Code : Now easily get bank ifsc code which you need while online transfer.
  2. All India PIN Code : Get all india PIN Code Numbers on 1 Android App.
  3. RTO Code : Get all India RTO Code on 1 Android App.
  4. Google 2 Step Code Speak : Now app will speak your google 2 step code, so no need to check mobile, code will reach you.
  5. All Gujarat ST Bus Stand Helpline Numbers (GSRTC Helpline Numbers)
  6. Mumbai City Bus Numbers (MSRTC Helpline Numbers)

  7. Andhra Pradesh City Bus Helpline Numbers (APSRTC Helpline Numbers)

  8. Bhavnagar Diary (Bhavnagar Business Directory)

  9. Baroda Diary (Baroda Business Directory)
  10. Surat Diary (Surat Business Directory)
  11. Ahmedabad Diary (Ahmedabad Business Directory)
  12. Rajkot Diary (Rajkot Business Directory)

Adarsh Patel

My Self Adarsh Suryakant Patel. Currently I am working as Assistant Professor at Parul Instiute of Engineering and Technology, MCA Department, Limda, Vadodara. Major Responsibility : * Lectures * Organizing Training and Expert Sessions for * Placement and Recruitment * Faculty Representative