Last active
December 18, 2015 05:53
-
-
Save vedhavyas/b4b63f459de3664a9607 to your computer and use it in GitHub Desktop.
This code will fetch user phone number from contact card as well as accounts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private ArrayList<String> getOwnerPhone() { | |
ArrayList<String> numbers = new ArrayList<>(); | |
getPhoneFromContacts(numbers); | |
getPhoneFromProfile(numbers); | |
return numbers; | |
} | |
private void getPhoneFromContacts(ArrayList<String> numbers) { | |
final AccountManager manager = AccountManager.get(this); | |
final Account[] accounts = manager.getAccountsByType("com.google"); | |
String accountName, id, number; | |
if (accounts[0].name != null) { | |
accountName = accounts[0].name; | |
ContentResolver cr = getContentResolver(); | |
Cursor emailCur = cr.query( | |
ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, | |
ContactsContract.CommonDataKinds.Email.DATA + " = ?", | |
new String[]{accountName}, null); | |
if (emailCur != null) { | |
if (emailCur.moveToFirst()) { | |
do { | |
id = emailCur | |
.getString(emailCur | |
.getColumnIndex(ContactsContract.CommonDataKinds.Email.CONTACT_ID)); | |
if (id != null) { | |
Cursor pCur = cr.query( | |
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, | |
null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID | |
+ " = ?", new String[]{id}, null); | |
if (pCur != null) { | |
while (pCur.moveToNext()) { | |
number = pCur | |
.getString(pCur | |
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); | |
// this will check and format phone number | |
number = Utilities.getReadablePhoneNumber(number); | |
if (number != null && !numbers.contains(number)) { | |
numbers.add(number); | |
} | |
} | |
pCur.close(); | |
} | |
} | |
} while (emailCur.moveToNext()); | |
} | |
emailCur.close(); | |
} | |
} | |
} | |
private void getPhoneFromProfile(ArrayList<String> numbers) { | |
final ContentResolver content = getContentResolver(); | |
final Cursor cursor = content.query( | |
Uri.withAppendedPath( | |
ContactsContract.Profile.CONTENT_URI, | |
ContactsContract.Contacts.Data.CONTENT_DIRECTORY), | |
new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER}, | |
ContactsContract.Contacts.Data.MIMETYPE + "=?", | |
new String[]{ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE}, | |
null | |
); | |
String number; | |
if (cursor != null) { | |
String[] columns = cursor.getColumnNames(); | |
while (cursor.moveToNext()) { | |
for (String column : columns) { | |
number = cursor.getString(cursor.getColumnIndex(column)); | |
//check and format phone number | |
number = Utilities.getReadablePhoneNumber(number); | |
if (number != null && !numbers.contains(number)) { | |
numbers.add(number); | |
} | |
} | |
} | |
cursor.close(); | |
} | |
} | |
//permissions in manifest file | |
<uses-permission android:name="android.permission.READ_PROFILE" /> | |
<uses-permission android:name="android.permission.READ_CONTACTS" /> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment