I have an Android Dialer App in which I want to open contacts. I took the code from Stack Overflow itself. But there is an argument in the startActivityForResult
function, PICK_CONTACT
. It is showing error.
else if(id == R.id.action_contacts){
Intent i = new Intent(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(i, PICK_CONTACT);
}
And the on ActivityResult
function is like this
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode) {
case (PICK_CONTACT) :
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);
if (c.moveToFirst()) {
String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
// TODO Fetch other Contact details as you want to use
}
}
break;
}
}
Please help how to correct this error.
It's a request code. When calling startActivityForResult
, you need to pass in an argument that identifies your request. This same integer value is used later on in onActivityResult
to check which request you are returning from.
You should declare this, and any other request codes like this at the top of your activity.
static final int PICK_CONTACT = 1; // requestCode for Contact Picker intent
More details can be found here: https://developer.android.com/training/basics/intents/result.html