I am developing a simple messenger app using firebase(for backend). I wish to compare the contact phonenumbers of the user to the contact phone numbers stored in my database,so that user can view a refined list of contacts. I have looked up in google but I could not find a way to get the numbers stored in a separate variable. I tried the below code from another post in stack overflow but i am getting an error at getactivity saying "cannot resolve method"
ContentResolver cr = getActivity().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()) {
int phoneType = pCur.getInt(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
String phoneNumber = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
switch (phoneType) {
case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE:
Log.e(name + "(mobile number)", phoneNumber);
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_HOME:
Log.e(name + "(home number)", phoneNumber);
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_WORK:
Log.e(name + "(work number)", phoneNumber);
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_OTHER:
Log.e(name + "(other number)", phoneNumber);
break;
default:
break;
}
}
pCur.close();
}
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Most probably if you are using that code inside Activity than i think getActivity() is not required ,you can directly call getContentResolver() but if still doesn't work than try this to get contacts and other details . I am copying code from one of my project so hope you will understand that
hope this can help