Android retrieving phone contacts query takes long time - java

Hello i am using the next code for retrieving phone contacts name,number and its taked to long.
After littel search i found the the cursor.moveToFirst() command is that one who cuz this .
But i couldent find any solution ): may some one help me please ?
Thanks !
public void loadPhoneMembers(){
ContentResolver cr = this.getContentResolver();
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if(cursor.moveToFirst())
{
do
{
String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
if(Integer.parseInt(cursor.getString(cursor.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 contactNumber=pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String contactName =
pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
if(!phonesAdded.contains(contactNumber)){
members.add(contactName+","+contactNumber); // members is arrayList
}
break;
}
pCur.close();
}
} while (cursor.moveToNext()) ;
}
}

You are querying one extra Cursor for each Contact from your first query. You can query the Names and TelephoneNumbers in one query and ignore the Contacts without Telephone number.
String[] projection = {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,ContactsContract.CommonDataKinds.Phone.NUMBER ,ContactsContract.CommonDataKinds.Phone.PHOTO_URI};
Cursor cursor = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,projection, null, null, null);
Also, while (cursor.moveToNext()){
}
has same effect as the following.
if(cursor.moveToFirst())
{
do
{
} while (cursor.moveToNext()) ;

Related

How to get all contacts in Android and ignore email-only contacts

Below is the code I am using to fetch contacts in Android.
String[] projectionFields = new String[]{
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
};
CursorLoader cursorLoader = new CursorLoader(context,
ContactsContract.Contacts.CONTENT_URI,
projectionFields, // the columns to retrieve
SELECTION, // the selection criteria (none)
null, // the selection args (none)
null // the sort order (default)
);
Cursor cursor = cursorLoader.loadInBackground();
if (cursor.moveToFirst()) {
int nameIndex = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
do {
String contactDisplayName = cursor.getString(nameIndex);
} while (cursor.moveToNext());
}
I am also getting a lot of email only contacts. I don't want to show email-only contacts. How can I go about this?
I understand from your question that you need to get the contacts that has phone numbers. If this is the case, you can check if the contact has phone number by
ContentResolver contentResolver = getContentResolver();
ContentProviderClient mCProviderClient = contentResolver.acquireContentProviderClient(ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
Cursor cursor = mCProviderClient.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, PROJECTION, null, null, "UPPER(" + ContactsContract.Contacts.DISPLAY_NAME + ") ASC");
if (cursor.getCount() > 0) {
while (cursor.moveToNext()) {
String contactId =
cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)));
if (hasPhoneNumber > 0) {
Cursor phones = mCProviderClient.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, PROJECTION_PHONE,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);
while (phones.moveToNext()) {
String number = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
phones.close();
}
}
mCProviderClient.close();
cursor.close();
}
The value of hasPhoneNumber will be > 0, if there is a phone number assigned to the contact.
Please refer to this answer, it will allow only numbers and will also remove duplication.
Hope you find it helpful and your problem gets resolved.
Thanks.

Android SQLlite Get List of Columns from Empty Table

Using Android SQL, I'm trying to get a list of the columns from an SQL table which is empty.
Here is what I have tried so far:
SQLiteDatabase db = getReadableDatabase();
ArrayList<String> output = new ArrayList<String>();
Cursor ti = db.rawQuery("PRAGMA table_info("+myTable+")", null);
if ( ti.moveToFirst() ) {
do {
output.add(ti.getString(1));
} while (ti.moveToNext());
}
ti.close();
db.close();
return output;
The resulting list Appears just the word INTEGER, while the database has many columns. How can I make this work?
this is i use to retrieve column name in string:
SQLiteDatabase mDataBase;
(some code here...)
mDataBase = getReadableDatabase();
Cursor dbCursor = mDataBase.query(TABLE_NAME, null, null, null, null, null, null);
String[] columnNames = dbCursor.getColumnNames();
or this :
Cursor c = db.rawQuery("SELECT * FROM table WHERE 0", null);
try {
String[] columnNames = c.columnNames();
} finally {
c.close();
}
The column name is returned in the column named name:
...
int nameColIdx = ti.getColumnIndexOrThrow("name");
...
output.add(ti.getString(nameColIdx));
...

Android ContactsProvider: How to get name and phone number in one row for specific contact?

I want to retrieve a cursor where in one row contains both name and phone number. Is the way to do this pretty, without any loops?
Uri uri = ContactsContract.Data.CONTENT_URI;
getContentResolver().query(uri,
new String[] {ContactsContract.Data._ID, Phone.NUMBER, StructuredName.GIVEN_NAME}, null, null, null)
the code i use to get my contacts is:
Cursor cursor = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,null, null);
String Contactos =" CONTACTOS DEL TELEFONO "+cursor.getCount()+"\n";
while (cursor.moveToNext()) {
String name =cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Contactos+="Nombre: "+name+"\n"+
"Telefono: "+phoneNumber+"\n"+
"----------------------------\n";
}
then add the Contactos to a textview, and that's all!
see ya!

Android SDK-Get specific contacts from contact list

I'm trying to query the contacts table to get all the contacts of a specific name, for example all the contacts with first name "George"
At the moment I get all the contacts with this
Cursor contactsCursor = managedQuery(contactData,
null, null, null, null);
Any help is appriciated :)
Cursor cursor = managedQuery(ContactsContract.Data.CONTENT_URI, null,
ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME + " = ?",
new String[] { "George" }, null);

Android - How to get phone # from contacts

I want to get a phone number to dial from contacts , here is the code I'm testing:
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
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
}
pCur.close();
}
For some reason it gets error at 'new Sting[]{id}, null);
Eclipse error #1 -id cannot be resolved to a variable
Eclipse error #2 -type mismatch: cannot convert from ArrayList to String
It appears maybe, as im a noob, 'id' is already being used by ArrayList ? But when i change 'id' to 'id2' Elcipse still dosent like it...
Please advise?
Thanks,
FlinxSYS
Here is working code for you:
public static void getContacts(ContentResolver cr) {
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
// read id
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
// read names
String displayName = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
// Phone Numbers
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id }, null);
while (pCur.moveToNext()) {
String number = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String typeStr = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
}
pCur.close();
...
Reference : http://ganeshtiwaridotcomdotnp.blogspot.com/2011/10/android-code-for-reading-phone-contacts.html

Categories