I'm trying to get contact numbers using getContentResolver().query() method. but it not get any numbers. here is my code and app file.
Activity Class
abc = (Button) findViewById(R.id.button);
abc.setOnClickListener(new View.OnClickListener() {
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onClick(View v) {
ContentResolver contentResolver = getContentResolver();
Cursor cursor = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
try {
if (cursor.getCount() > 0) {
while (cursor.moveToNext()) {
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String contact_Id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String Numbers = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Cursor cursor_phone = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?" , new String[]{contact_Id}, null);
while (cursor_phone.moveToNext()) {
String phNumber = cursor_phone.getString(cursor_phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Numbers = phNumber;
int PHONE_TYPE = cursor_phone.getInt(cursor_phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
switch (PHONE_TYPE) {
case ContactsContract.CommonDataKinds.Phone.TYPE_HOME:
Numbers += "Home" + phNumber + "\n";
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE:
Numbers += "Mobile" + phNumber + "\n";
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_WORK:
Numbers += "Work" + phNumber + "\n";
break;
}
}
Toast toast = Toast.makeText(Context, name + Numbers, Toast.LENGTH_SHORT);
toast.show();
}
} else {
Toast toast = Toast.makeText(a, "Nothing", Toast.LENGTH_SHORT);
toast.show();
}
}
catch (Exception Ex)
{
Log.d(Ex.toString(),"");
}
}
});
Build Gradle File
apply plugin: 'com.android.application'
android {
compileSdkVersion 20
buildToolsVersion "20.0.0"
defaultConfig {
applicationId "com.metrolixsolutions.contactgetter"
minSdkVersion 14
targetSdkVersion 20
versionCode 1
versionName "1.0"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
It doesn't get any numbers to cursor_phone.
You don't need the second query