my app must picks one contact.
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent pickContact = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);
startActivityForResult(pickContact, REQUEST_CONTACT);
}
});
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (resultCode != Activity.RESULT_OK) {
return;
}
if (requestCode == REQUEST_CONTACT && intent != null) {
Uri contactUri = intent.getData();
String[] queryFields = new String[]{Contacts.DISPLAY_NAME_PRIMARY} ;
Cursor c = getActivity().getContentResolver().query(contactUri, queryFields, null, null, null);
try {
if (c.getCount() == 0) {
return;
}
c.moveToFirst();
String contact = c.getString(0);
mButton.setText(contact);
} finally {
c.close();
}
}
}
But it works fine only with contacts stored in my phone. If contact stored in Google account, it wouldn't work. I'll get empty cursor. I've read this and this. And i've read Google Contacts API. These methods are completely different. What should i do if i want get data from any contacts (Google, phone, sim) by pressing single button?