简体   繁体   中英

Picking a Contact from phone book in android

I want to pick a contact from the phone book in android. I press a button and then it shows the contact list. When I click I want to pick that clicked contact's number displayed in my activity, but in my case it returned null. Here is my code:

    import android.app.Activity;
    import android.content.Intent;
    import android.database.Cursor;
    import android.net.Uri;
    import android.os.Bundle;
    import android.provider.Contacts.People;
    import android.provider.ContactsContract.Contacts;
    import android.provider.ContactsContract.PhoneLookup;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.TextView;

    public class Main extends Activity {
    Button b;
    TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = (TextView) findViewById(R.id.textView1);
        b = (Button) findViewById(R.id.button1);
        b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(Intent.ACTION_PICK,People.CONTENT_URI);
                startActivityForResult(intent, 100);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Uri contact = data.getData();
        Cursor c = managedQuery(contact, null, null, null, null);
        c.moveToFirst();
        tv.setText(c.getString(c.getColumnIndex(People.NUMBER))+" Added");
    }

}

Why this is happening? Thanks in advance.

instead of using People.CONTENT_URI use ContactsContract.Contacts.CONTENT_URI
that is instead of

 Intent intent = new Intent(Intent.ACTION_PICK,People.CONTENT_URI);

use

 Intent intent = new Intent(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI);

example : Getting Contact Phone Number

your onacitivityResult:

  Uri contact = data.getData();
      ContentResolver cr = getContentResolver();

    Cursor c = managedQuery(contact, null, null, null, null);
    //      c.moveToFirst();


    while(c.moveToNext()){
        String id = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));

        String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
        if (Integer.parseInt(c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
            Cursor pCur = cr.query(Phone.CONTENT_URI,null,Phone.CONTACT_ID +" = ?", new String[]{id}, null);

            while(pCur.moveToNext()){
                String phone = pCur.getString(pCur.getColumnIndex(Phone.NUMBER));
                tv.setText(name+" Added " + phone);
            }
        }

    }

Did you try to pick different contacts? this may be stupid, but maybe that contact doesn't have a number?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM