Get Contacts from Phone only android - android

In my app i need contacts from phone only not from other accounts like gmail and Facebook.With my code i am getting the contacts but issue is that i am getting it from all the sources like gmail ,facebook etc.
Code
contactResolver = context.getContentResolver();
Cursor cur = context.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
contactsModel = new ContactsModel();
id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
contactsModel.name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
phoneCursor = contactResolver.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[]{id}, null);
while (phoneCursor.moveToNext()) {
// Do something with phones
int phoneType = phoneCursor.getInt(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
String phoneNo = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
//you will get all phone numbers according to it's type as below switch case.
switch (phoneType) {
case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE:
phone = phoneNo;
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_HOME:
phone = phoneNo;
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_WORK:
phone = phoneNo;
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_WORK_MOBILE:
phone = phoneNo;
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_OTHER:
phone = phoneNo;
break;
default:
phone = phoneNo;
break;
}
listPhones.add(phoneNo);
}
/*get email*/
emailCursor = contactResolver.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", new String[]{id}, null);
while (emailCursor.moveToNext()) {
//String emai = emailCursor.getString(emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
int type = emailCursor.getInt(emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
String s = (String) ContactsContract.CommonDataKinds.Email.getTypeLabel(context.getResources(), type, "");
listEmails.add(emailCursor.getString(emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)));
}
contactsModel.emails = listEmails;
contactsModel.numbers = listPhones;
listContacts.add(contactsModel);
listEmails = new ArrayList<>();
listPhones = new ArrayList<>();
emailCursor.close();
phoneCursor.close();
}
}
cur.close();
return listContacts;
So how will i get the contacts from only phonebook.

Try below query:
Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, ContactsContract.Contacts.IN_VISIBLE_GROUP + "=1",
null,
null
);
where, IN_VISIBLE_GROUP: 1=local, 0=google account

Hope it will help you
public void readContacts() {
StringBuffer sb = new StringBuffer();
sb.append("......Contact Details.....");
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
null, null, null);
String phone = null;
String emailContact = null;
String emailType = null;
String image_uri = "";
Bitmap bitmap = null;
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur
.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur
.getString(cur
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
image_uri = cur
.getString(cur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
if (Integer
.parseInt(cur.getString(cur
.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
System.out.println("name : " + name + ", ID : " + id);
sb.append("\n Contact Name:" + name);
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = ?", new String[] { id }, null);
while (pCur.moveToNext()) {
phone = pCur
.getString(pCur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
sb.append("\n Phone number:" + phone);
System.out.println("phone" + phone);
}
pCur.close();
Cursor emailCur = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID
+ " = ?", new String[] { id }, null);
while (emailCur.moveToNext()) {
emailContact = emailCur
.getString(emailCur
.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
emailType = emailCur
.getString(emailCur
.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
sb.append("\nEmail:" + emailContact + "Email type:" + emailType);
System.out.println("Email " + emailContact
+ " Email Type : " + emailType);
}
emailCur.close();
}
if (image_uri != null) {
//System.out.println(Uri.parse(image_uri));
try {
bitmap = MediaStore.Images.Media
.getBitmap(this.getContentResolver(),
Uri.parse(image_uri));
sb.append("\n Image in Bitmap:" + bitmap);
// System.out.println(bitmap);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
sb.append("\n........................................");
}
textDetail.setText(sb);
}
}

Related

get All Contacts from Mobile android

I want to get all contact number from mobile
I am using the following code but it is giving only contact numbers which are in mobile not of sim
private void getContactList() {
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
if ((cur != null ? cur.getCount() : 0) > 0) {
while (cur != null && cur.moveToNext()) {
String id = cur.getString(
cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(
ContactsContract.Contacts.DISPLAY_NAME));
if (cur.getInt(cur.getColumnIndex(
ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0) {
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[]{id}, null);
while (pCur.moveToNext()) {
String phoneNo = pCur.getString(pCur.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER));
ContactsPlacer obj = new ContactsPlacer();
obj.setContactname(name);
obj.setContactnumber(phoneNo);
names.add(obj);
}
pCur.close();
}
}
}
if(cur!=null){
cur.close();
}
}
Below is code for getting all contact from device
private void getAllContacts() {
try {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Read Contact permission not enabled", Toast.LENGTH_SHORT).show();
}
else {
contactVOList = new ArrayList();
ContactVO contactVO;
ContentResolver contentResolver = getContentResolver();
Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
if (cursor.getCount() > 0) {
while (cursor.moveToNext()) {
int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)));
if (hasPhoneNumber > 0) {
String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
contactVO = new ContactVO(pref.getUser_ID(),phoneNumberArrayList);
contactVO.setContactName(name);
Cursor phoneCursor = contentResolver.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[]{id},
null);
if (phoneCursor.moveToNext()) {
String phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
phoneNumber = phoneNumber.replace(" ", "");
phoneNumber = phoneNumber.trim();
String number = "+91#1234*121#";
number=phoneNumber.replaceAll("[\\D]", "");
contactVO.setContactNumber(number);
phoneNumberArrayList.add(number);
}
phoneCursor.close();
Cursor emailCursor = contentResolver.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
new String[]{id}, null);
while (emailCursor.moveToNext()) {
String emailId = emailCursor.getString(emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
}
emailCursor.close();
contactVOList.add(contactVO);
}
}
cursor.close();
if (contactVOList.size() > 0) {
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
CallAPI();
}
}, 1000);
}
}
}
} catch (NumberFormatException e) {
e.printStackTrace();
}
}
and Added below code for get SIM contact
Uri simUri = Uri.parse("content://icc/adn");Cursor cursorSim = this.getContentResolver().query(simUri, null, null,null, null);
while (cursorSim.moveToNext()) {
listName.add(cursorSim.getString(cursorSim.getColumnIndex("name")));
listContactId.add(cursorSim.getString(cursorSim.getColumnIndex("_id")));
listMobileNo.add(cursorSim.getString(cursorSim.getColumnIndex("number")));
}

how to separate multiple phones from a single contact

how can I separate the phones from a contact if it has multiple numbers, as seen in the image
MG8ou.png
here my code
#Override
protected Void doInBackground(Void... params) {
ContentResolver contentResolver = getActivity().getContentResolver();
Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
if (cursor.getCount() > 0) {
while (cursor.moveToNext()) {
int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)));
if (hasPhoneNumber > 0) {
String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
ContactObjets contactObjets = new ContactObjets();
contactObjets.setContactName(name);
Cursor phoneCursor = contentResolver.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[]{id},
null);
if (phoneCursor.moveToNext()) {
String phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
contactObjets.setContactNumber(phoneNumber);
}
phoneCursor.close();
Cursor emailCursor = contentResolver.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
new String[]{id}, null);
while (emailCursor.moveToNext()) {
String emailId = emailCursor.getString(emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
contactObjets.setContactEmail(emailId);
}
emailCursor.close();
contactObjetsList.add(contactObjets);
}
}
}
return null;
}
I have it inside an AsyncTask that is this asu you see inside a dialog but it only calls the first number, but if it has more than one number the contact does not do it.
how can i solve my problem
#Override
protected Void doInBackground(Void... params) {
ContentResolver contentResolver = getActivity().getContentResolver();
Cursor cursor = contentResolver.query(
Contacts.CONTENT_URI,
null,
null,
null,
Phone.DISPLAY_NAME + " ASC");
while (cursor.moveToNext()) {
String id = cursor.getString(cursor.getColumnIndex(Contacts._ID));
String name = cursor.getString(cursor.getColumnIndex(Contacts.DISPLAY_NAME));
Cursor phoneCursor = contentResolver.query(
Phone.CONTENT_URI,
null,
Phone.CONTACT_ID + " = ?",
new String[]{id},
null);
if (phoneCursor.moveToNext()) {
for (int i = 0; phoneCursor.getCount() > i; i++ ){
phoneCursor.moveToPosition(i);
String phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(Phone.NUMBER));
int typePhone = phoneCursor.getInt(phoneCursor.getColumnIndex(Phone.TYPE));
String type = "";
switch (typePhone){
case ContactsContract.CommonDataKinds.Phone.TYPE_HOME:
type = "Casa";
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE:
type = "Celular";
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_WORK:
type = "Trabajo";
break;
default:
type = "Otro";
break;
}
ContactObjets contactObjets = new ContactObjets();
contactObjets.setContactName(name);
contactObjets.setContactNumber(phoneNumber);
contactObjets.setContactType(type);
contactObjetsList.add(contactObjets);
}
}
phoneCursor.close();
}
return null;
}

how to get the contact number from contacts in android?

In my application i want to get the contact number from contacts and print it on the edit text. I wrote code from google. but it is not working.
java:
private void browseContacts() {
Intent pickContactIntent = new Intent(Intent.ACTION_PICK,
Uri.parse("content://contacts"));
pickContactIntent.setType(Phone.CONTENT_TYPE); // Show user only
// contacts w/ phone
// numbers
startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request it is that we're responding to
Log.v("TAG_CONTACTNAME",""+POSTPAID_SERVICETYPEID);
if (requestCode == PICK_CONTACT_REQUEST) {
// Make sure the request was successful
if (resultCode == getActivity().RESULT_OK) {
Cursor cursor = null;
try {
cursor = getActivity().getContentResolver().query(Phone.CONTENT_URI, null, null, null, null);
int phoneNumberIdx = cursor.getColumnIndex(Phone.NUMBER);
cursor.moveToFirst();
do {
String phoneNumber = cursor.getString(phoneNumberIdx);
Log.v("TAG_CONTACTNAME",""+phoneNumber);
} while (cursor.moveToNext()); }
catch (Exception e) { e.printStackTrace();
} finally {
if (cursor != null) { cursor.close();
} }
}
}
}
please help me
Just call this method(GetAllContacts)
public void GetAllContacts(){
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
new String[]{id}, null);
while (pCur.moveToNext()) {
String phone = pCur.getString(
pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
contactsNumber.add(phone);
Log.d("Number is", phone);
}
pCur.close();
}
}
}
}
And add this in menifest
<uses-permission android:name="android.permission.READ_CONTACTS" />
Use this in onActivityResult
Cursor cursor = null;
try {
cursor = context.getContentResolver().query(Phone.CONTENT_URI, null, null, null, null);
int contactIdIdx = cursor.getColumnIndex(Phone._ID);
int nameIdx = cursor.getColumnIndex(Phone.DISPLAY_NAME);
int phoneNumberIdx = cursor.getColumnIndex(Phone.NUMBER);
int photoIdIdx = cursor.getColumnIndex(Phone.PHOTO_ID);
cursor.moveToFirst();
do {
String idContact = cursor.getString(contactIdIdx); String name = cursor.getString(nameIdx);
String phoneNumber = cursor.getString(phoneNumberIdx);
//...
} while (cursor.moveToNext());
}
catch (Exception e) {
e.printStackTrace();
} finally {
if (cursor != null) {
cursor.close();
}
}
and also this in your manifest,
<uses-permission android:name="android.permission.READ_CONTACTS" />
Go on this link see all code step by step getting all contacts list whatever you need to it like as numbers, email, notes etc...
http://www.coderzheaven.com/2011/06/13/get-all-details-from-contacts-in-android/
This method put in your onCreate method,
public void readContacts(){
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
System.out.println("name : " + name + ", ID : " + id);
// get the phone number
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
new String[]{id}, null);
while (pCur.moveToNext()) {
String phone = pCur.getString(
pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
System.out.println("phone" + phone);
}
pCur.close();
// get email and type
Cursor emailCur = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
new String[]{id}, null);
while (emailCur.moveToNext()) {
// This would allow you get several email addresses
// if the email addresses were stored in an array
String email = emailCur.getString(
emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
String emailType = emailCur.getString(
emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
System.out.println("Email " + email + " Email Type : " + emailType);
}
emailCur.close();
// Get note.......
String noteWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
String[] noteWhereParams = new String[]{id,
ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE};
Cursor noteCur = cr.query(ContactsContract.Data.CONTENT_URI, null, noteWhere, noteWhereParams, null);
if (noteCur.moveToFirst()) {
String note = noteCur.getString(noteCur.getColumnIndex(ContactsContract.CommonDataKinds.Note.NOTE));
System.out.println("Note " + note);
}
noteCur.close();
//Get Postal Address....
String addrWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
String[] addrWhereParams = new String[]{id,
ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE};
Cursor addrCur = cr.query(ContactsContract.Data.CONTENT_URI,
null, null, null, null);
while(addrCur.moveToNext()) {
String poBox = addrCur.getString(
addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POBOX));
String street = addrCur.getString(
addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET));
String city = addrCur.getString(
addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY));
String state = addrCur.getString(
addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION));
String postalCode = addrCur.getString(
addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE));
String country = addrCur.getString(
addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY));
String type = addrCur.getString(
addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.TYPE));
// Do something with these....
}
addrCur.close();
// Get Instant Messenger.........
String imWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
String[] imWhereParams = new String[]{id,
ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE};
Cursor imCur = cr.query(ContactsContract.Data.CONTENT_URI,
null, imWhere, imWhereParams, null);
if (imCur.moveToFirst()) {
String imName = imCur.getString(
imCur.getColumnIndex(ContactsContract.CommonDataKinds.Im.DATA));
String imType;
imType = imCur.getString(
imCur.getColumnIndex(ContactsContract.CommonDataKinds.Im.TYPE));
}
imCur.close();
// Get Organizations.........
String orgWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
String[] orgWhereParams = new String[]{id,
ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE};
Cursor orgCur = cr.query(ContactsContract.Data.CONTENT_URI,
null, orgWhere, orgWhereParams, null);
if (orgCur.moveToFirst()) {
String orgName = orgCur.getString(orgCur.getColumnIndex(ContactsContract.CommonDataKinds.Organization.DATA));
String title = orgCur.getString(orgCur.getColumnIndex(ContactsContract.CommonDataKinds.Organization.TITLE));
}
orgCur.close();
}
}
}
}
I hope solution you have.
Last one very important permission add in Manifest xml file
<uses-permission android:name="android.permission.READ_CONTACTS" />

Fetch Local phonebook contacts From SIM card only android

I want to know if its possible to fetch contacts which exist in SIM card or phonebook only. Right now I am using the following code to fetch contacts and it fetches all the contacts even my gmail and Facebook Contacts.
Cursor cursor = getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI, null, null, null, Phone.DISPLAY_NAME + " ASC");
if (cursor.getCount() > 0)
{
while (cursor.moveToNext())
{
PhoneBookUserEntity user = new PhoneBookUserEntity();
// Pick out the ID, and the Display name of the
// contact from the current row of the cursor
user.setId(cursor.getString(cursor.getColumnIndex(BaseColumns._ID)));
user.setPhoneBookName(cursor.getString(cursor.getColumnIndex(
ContactsContract.Contacts.DISPLAY_NAME)));
if(user.getphonebookname().length() > 4)
username = user.getphonebookname();//.substring(0,4);
else
username = user.getphonebookname();//.substring(0,1);
String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
// if (Boolean.parseBoolean(hasPhone)) {
Cursor phones = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ user.getId(), null, null);
while (phones.moveToNext()) {
user.sePhoneNumber(phones.getString(phones.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER)));
}
phones.close();
//}
// user.sePhoneNumber(cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + user.getId(), null, null);
while (emails.moveToNext()) {
// This would allow you get several email addresses
user.setEmailAddress(emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)));
}
emails.close();
user.setImageURI(getPhotoUri(user.getId()));
SearchContactsActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
_progressDialog.setMessage("Copying over your local phone book. Retrieving contact information for \n"+ username.toUpperCase());
}
});
arraylist.add(user);
}
}
cursor.close();
For Sim contact only you can use below code
private void allSIMContact()
{
try
{
String ClsSimPhonename = null;
String ClsSimphoneNo = null;
Uri simUri = Uri.parse("content://icc/adn");
Cursor cursorSim = this.getContentResolver().query(simUri,null,null,null,null);
Log.i("PhoneContact", "total: "+cursorSim.getCount());
while (cursorSim.moveToNext())
{
ClsSimPhonename =cursorSim.getString(cursorSim.getColumnIndex("name"));
ClsSimphoneNo = cursorSim.getString(cursorSim.getColumnIndex("number"));
ClsSimphoneNo.replaceAll("\\D","");
ClsSimphoneNo.replaceAll("&", "");
ClsSimPhonename=ClsSimPhonename.replace("|","");
Log.i("PhoneContact", "name: "+ClsSimPhonename+" phone: "+ClsSimphoneNo);
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
Cursor cursor = getContacts();
getBundleValues2();
String displayName = "";
while (cursor.moveToNext()) {
//taking id name and phone number from contacts using content provider
String displayid = cursor.getString(cursor
.getColumnIndex(ContactsContract.Contacts._ID));
displayName = cursor.getString(cursor
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String displayphnno = "";
ContentResolver cr = getContentResolver();
if (Integer.parseInt(cursor.getString(cursor
.getColumnIndex(ContactsContract.Data.HAS_PHONE_NUMBER))) > 0) {
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = ?", new String[] { displayid }, null);
while (pCur.moveToNext()) {
displayphnno = pCur
.getString(pCur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))
+ "\n";
ContactProviderViewGroup v6;
//using view group appending it to an activity
if (colorFlag) {
v6 = new ContactProviderViewGroup(this, displayName,
displayphnno, passedid, colorFlag);
colorFlag = false;
} else {
v6 = new ContactProviderViewGroup(this, displayName,
displayphnno, passedid, colorFlag);
colorFlag=true;
}
LL1.addView(v6);
}
pCur.close();
}
}
}
private void getBundleValues2() {
Intent i = getIntent();
Bundle myBundle = i.getExtras();
if (myBundle != null) {
passedid = myBundle.getInt("gid");
}
}
private Cursor getContacts() {
// Run query
Uri uri = ContactsContract.Contacts.CONTENT_URI;
String[] projection = new String[] { ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.HAS_PHONE_NUMBER };
String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '"
+ ("1") + "'";
String[] selectionArgs = null;
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME
+ " COLLATE LOCALIZED ASC";
return managedQuery(uri, projection, selection, selectionArgs,
sortOrder);
}

android phone contact

i need to read organization name from phone contacts in 2.1 i can read name and phone number using following code
Cursor cur = User.cr.query(ContactsContract.Contacts.CONTENT_URI, null,
null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
// ID AND NAME FROM CONTACTS CONTRACTS
String id = cur.getString(cur
.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur
.getString(cur
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
// Log.i("Pratik", "ID :" + id);
// Log.i("Pratik", "NAME :" + name);
// GET PHONE NUMBERS WITH QUERY STRING
if (Integer
.parseInt(cur
.getString(cur
.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor pCur = User.cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = ?", new String[] { id }, null);
// WHILE WE HAVE CURSOR GET THE PHONE NUMERS
HashMap tempMap=new HashMap();
while (pCur.moveToNext()) {
// Do something with phones
phone = pCur
.getString(pCur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA));
String phoneType = pCur
.getString(pCur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
tempMap.put(phoneType, phone);
}
pCur.close();
if(name!=null&&tempMap!=null)
if (!(Pattern.matches("^[A-Za-z0-9.\\-]+.[A-Za-z0-9.\\-]+\\.[A-Za-z]{2,4}$",name)))
{ User.provider_name_phoneno.put(name, tempMap);
User.phoneContactName.add(name);
}
}
}
}
Organization has its own MIME type, so you'll also need to query for that.. This should help:
public Organization getContactOrg(String id) {
Organization org = new Organization();
String where = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
String[] whereParameters = new String[]{id,
ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE};
Cursor orgCur = this.cr.query(ContactsContract.Data.CONTENT_URI, null, where, whereParameters, null);
if (orgCur.moveToFirst()) {
String orgName = orgCur.getString(orgCur.getColumnIndex(ContactsContract.CommonDataKinds.Organization.DATA));
String title = orgCur.getString(orgCur.getColumnIndex(ContactsContract.CommonDataKinds.Organization.TITLE));
if (orgName.length() > 0) {
org.setOrganization(orgName);
org.setTitle(title);
}
}
orgCur.close();
return(org);
}

Categories

Resources