How to get all sync contacts(google + facebook) in android - android

I have used cursor for getting all contacts from android contact list as :
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) {
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id }, null);
while (pCur.moveToNext()) {
phoneContactList.add(name);
Log.i("Contact List", name);
}
pCur.close();
}
}
My contact list is in sync with facebook. But in the contact list I am not getting contacts from facebook.
How can I get all sync contacts..?

just in case someone stumbles upon this, the answer is here:
https://stackoverflow.com/a/8138587/669180
"Facebook is not included in the ContactPicker because Facebook forbid that."

Related

specific api for accessing contacts app in android

I am making an app, in that app, the user will speak CALL (person name).
But, I am not able to understand how to access contacts and call that person automatically.
Till now, I am able to call a particular number only.
My code is mentioned below.
if(msg.indexOf("call")!=-1){
Intent i2=new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+"123456789"));
startActivity(i2);
}
private void getContactList() {
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
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));
Log.i(TAG, "Name: " + name);
Log.i(TAG, "Phone Number: " + phoneNo);
}
pCur.close();
}
}
}
if(cur!=null){
cur.close();
}
}
You can refer this stackoverflow links:
android get all contacts
http://saigeethamn.blogspot.com/2011/05/contacts-api-20-and-above-android.html
https://developer.android.com/training/contacts-provider/retrieve-names
You can use below library to fetch contacts
implementation 'com.github.tamir7.contacts:contacts:1.1.7'
https://github.com/tamir7/Contacts

Get contacts from android device filter by account type

I writen a application contact sync. It sync contact from server to device. When sync done with user haduy#lvsolution.vn, i want get all contact's haduy user. But it get all contact in device, don't filter by haduy user. I only want get contact of haduy#lvsolution account.
public void getContact()
{
String phone = null;
String name = null;
listContact.clear();
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));
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()) {
phone = pCur.getString(
pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
System.out.println("phone" + phone);
}
Contact contact = new Contact(name,phone);
listContact.add(contact);
pCur.close();
}
}
}
}

How can i get all phone numbers from call log to a custom List view in Android?

I want to access all phone numbers from android phone call log to a Custom Listview. Custom ListView already created and working fine with accessing all phone numbers from Phone contacts. Could any body please explain ?
Try this
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) {
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));
Toast.makeText(ArticleActivity.this, "Name: " + name + ", Phone No: " + phoneNo, Toast.LENGTH_SHORT).show();
}
pCur.close();
}
}
}

Contact ListVew Working In Emulator But Not in Real Device

I have made an application in Android 2.2 that Displays only three things in listview. All Phone book, Contacts, and Number in ListView with CheckBox. When I Run The App in Emulator 2.2 then it is working properly, but when I use the real device to test an application then it will only display contacts Email ids and when CheckBoxes are used the app crashes.
I want clear that I don't call email ids from anywhere. I have used the code below to call fetch contact details from Phone book.
In BaseAdapter Class :
ContentResolver cr = getContentResolver();
cr = context.getContentResolver();
cursor = cr.query(
ContactsContract.Contacts.CONTENT_URI,
null,
null,
null,
null);
public View getView(final int position, View convertView, ViewGroup parentView)
{
if(cursor != null){
while(cursor.moveToNext()){
int nameFiledColumnIndex = cursor.getColumnIndex(
ContactsContract.Contacts.DISPLAY_NAME);
String id = cursor.getString(
cursor.getColumnIndex(ContactsContract.Contacts._ID));
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
new String[]{id}, null);
while (pCur.moveToNext()) {
phoneNo = pCur.getString(pCur.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER));
}
Does anybody have any idea about what mistakes I have made ?
Try this one...
ContentResolver contentResolver = getContentResolver();
Cursor cur = contentResolver.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) {
// Query phone here. Covered next
Cursor phoneCur = contentResolver.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = ?", new String[] { id }, null);
while (phoneCur.moveToNext()) {
// Do something with phones
String phoneNo = phoneCur.getString(pCur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
contactDataset = new ContactDataset(); //This is object of dataset class to add the contacts.
contactDataset.setName(name);
contactDataset.setPhoneNumber(phoneNo);
getContactList.add(contactDataset);
}
pCur.close();
}
}
}
Let me know is it works or not.......

Retrieving Android Contacts

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 (name.equals(selected) && 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);
finalsend = ContactsContract.CommonDataKinds.Phone.NUMBER;
while (pCur.moveToNext()) {
}
pCur.close();
}
The code is supposed to look through contacts to find one that matches the "selected" variable which appears to work fine but then it is supposed to switch to the ContactsContract.CommonDataKinds.Phone.CONTENT_URI and find the same contact by matching up the ID's and give me the phone number saved for that contact.
Every time it returns "Data1", what am I doing wrong? It's probably a stupid mistake but any help is appreciated.
Following code snippet is working for me:
String id , name;
ContentResolver cr = getContentResolver();
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, sortOrder);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
name = cur.getString( cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Log.i(tag, "Id is "+ id+"\t Name is"+name);
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()) {
// Do something with phones
int phNumber = pCur.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER);
String phn = pCur.getString(phNumber);
Log.i("phn number", phn);
}
pCur.close();
}
}
}

Categories

Resources