Saturday, November 22, 2014

Get phone number from Contacts database, using Intent.ACTION_PICK

This example show how to get phone number from Contacts database, using Intent.ACTION_PICK.


MainActivity.java
package com.example.androidreadcontact;

import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {

 Button buttonReadContact;
 TextView textPhone;

 final int RQS_PICKCONTACT = 1;

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

  buttonReadContact = (Button) findViewById(R.id.readcontact);
  textPhone = (TextView) findViewById(R.id.phone);

  buttonReadContact.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    // Start activity to get contact
    final Uri uriContact = ContactsContract.Contacts.CONTENT_URI;
    Intent intentPickContact = new Intent(Intent.ACTION_PICK,
      uriContact);
    startActivityForResult(intentPickContact, RQS_PICKCONTACT);
   }
  });
 }

 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  if (resultCode == RESULT_OK) {
   if (requestCode == RQS_PICKCONTACT) {
    Uri returnUri = data.getData();
    Cursor cursor = getContentResolver().query(returnUri, null,
      null, null, null);

    if (cursor.moveToNext()) {
     int columnIndex_ID = cursor
       .getColumnIndex(ContactsContract.Contacts._ID);
     String contactID = cursor.getString(columnIndex_ID);

     int columnIndex_HASPHONENUMBER = cursor
       .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER);
     String stringHasPhoneNumber = cursor
       .getString(columnIndex_HASPHONENUMBER);

     if (stringHasPhoneNumber.equalsIgnoreCase("1")) {
      Cursor cursorNum = getContentResolver()
        .query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
          null,
          ContactsContract.CommonDataKinds.Phone.CONTACT_ID
            + "=" + contactID, null, null);

      // Get the first phone number
      if (cursorNum.moveToNext()) {
       int columnIndex_number = cursorNum
         .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
       String stringNumber = cursorNum
         .getString(columnIndex_number);
       textPhone.setText(stringNumber);
      }

     } else {
      textPhone.setText("NO Phone Number");
     }

    } else {
     Toast.makeText(getApplicationContext(), "NO data!",
       Toast.LENGTH_LONG).show();
    }
   }
  }
 }

}

/res/layout/activity_main.xml
<LinearLayout 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:orientation="vertical"
    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="com.example.androidreadcontact.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:autoLink="web"
        android:text="http://android-er.blogspot.com/"
        android:textStyle="bold" />

    <Button
        android:id="@+id/readcontact"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Read Contact" />

    <TextView
        android:id="@+id/phone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

Add permission of "android.permission.READ_CONTACTS" in AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.androidreadcontact"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />
    <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>

download filesDownload the files.


No comments: