public ArrayList<String> getPhoneNumber(Activity mActivity){ArrayList<String> numbers=null;numbers= new ArrayList<String>();Cursor c = mActivity.managedQuery(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);for(int i=0;i<c.getcount();i++){c.moveToPosition(i);String id = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts._ID));String hasPhone = c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));if (hasPhone.equalsIgnoreCase("1")){Cursor phones = mActivity.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id,null, null);if(phones.getCount()>0){phones.moveToFirst();String cNumber = phones.getString(phones.getColumnIndex("data1"));numbers.add(cNumber);}}}return numbers;}
Note:
Add Permission in Menifest.xml
<'uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
No comments:
Post a Comment