Question

In my app I want to access contacts from the phone and fill the form with some data getting from the contact. When I am trying to run the Intent, I can see the list of contacts in my phone but it doesn't send any data back and onActivityResult is not being called. Can someone please help me as I am a beginner in android programming.

Here is part of my code :

On click event of a button called 'Contacts'

btn_contacts.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            @SuppressWarnings("deprecation")
            Intent intent = new Intent(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_TYPE);                              
            startActivityForResult(intent, PICK_CONTACT);                   
        }
    });

In onActivityResult in the same activity.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    Log.e("REcAddress","in activity result");   
    super.onActivityResult(requestCode, resultCode, data);
    if(data!=null){
        Log.e("REcAddress","hey");  
    }
    if(requestCode == PICK_CONTACT && resultCode == RESULT_OK){
        Uri contactData = data.getData();   
        Log.e("REcAddress",contactData.toString());
        Cursor c = getContentResolver().query(contactData, null, null, null, null);
        if(c.moveToFirst()){
            String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            Log.e("REcAddress",name);   
            edt_rec_name.setText(name);
        }
    }
}

I have also attached an image to show the layout of the activity. I want to fill in as many details I can from contacts in the form. There is no error in Logcat too. Please, guide me.

Thanks.Layout File of activity

Was it helpful?

Solution

first add permissions:

 <uses-permission android:name="android.permission.READ_CONTACTS" />

then call intent on to pick contact on button click

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

private static int PICK_CONTACT = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button pick = (Button) findViewById(R.id.butpick);
    pick.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            pickContact();
        }
    });
}

private void pickContact() {
            //you have used CONTENT_TYPE and here should be  CONTENT_URI
    Intent intent = new Intent(Intent.ACTION_PICK,
            ContactsContract.Contacts.CONTENT_URI);
    startActivityForResult(intent, PICK_CONTACT);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, 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.getCount() > 0) {
                    while (c.moveToNext()) {
                    String name=c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                    edt_rec_name.setText(name);
                    }

        }   
    }

}

OTHER TIPS

You may not have the right permissions for accessing contacts on the phone. Are you using <uses-permission android:name="android.permission.READ_CONTACTS"/> in your Android Manifest?

Try the following code

Intent intent = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(intent, 1);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top