get Contact Phone Number from Context - Android Account

Android examples for Account:Contact Number

Description

get Contact Phone Number from Context

Demo Code


//package com.java2s;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

public class Main {
    @Nullable//from   w  w  w .j  a va 2 s  .com
    public static String getPhoneNumber(@NonNull Context context,
            @NonNull Intent data) {
        Cursor cursor = context
                .getContentResolver()
                .query(data.getData(),
                        new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER },
                        null, null, null);

        if (cursor != null)
            try {
                if (cursor.moveToFirst())
                    return cursor
                            .getString(cursor
                                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            } finally {
                cursor.close();
            }

        return null;
    }
}

Related Tutorials