如何打开联系人并在列表中显示已挑选的联系人?

时间:2016-10-07 08:54:13

标签: android android-fragments onclicklistener

我正在尝试按下按钮以打开我的联系人,让我选择我想要选择的联系人。然后,在列表中列出名称和(可能)他们的手机号码。到目前为止,我一直坚持用手机打开联系人。希望您能够帮助我。谢谢!这是我的代码:

public class RecipientsFragment extends Fragment {
public Button button;
static final int PICK_CONTACT=1;

public RecipientsFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_recipients, container, false);
    button = (Button) view.findViewById(R.id.button);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
            startActivityForResult(intent, PICK_CONTACT);

        }
    });
    return inflater.inflate(R.layout.fragment_recipients, container, false);
}
@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
    super.onActivityResult(reqCode, resultCode, data);

    switch (reqCode) {
        case (PICK_CONTACT) :
            if (resultCode == Activity.RESULT_OK) {
                String cNumber = null;
                Uri contactData = data.getData();
                Cursor c =  getActivity().managedQuery(contactData, null, null, null, null);
                if (c.moveToFirst()) {


                    String id =c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts._ID));


                    String hasPhone =c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

                    if (hasPhone.equalsIgnoreCase("1")) {
                        Cursor phones = getActivity().getContentResolver().query(
                                ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
                                ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id,
                                null, null);
                        phones.moveToFirst();
                        cNumber = phones.getString(phones.getColumnIndex("data1"));

                    }
                    String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                    System.out.println("number is:"+cNumber+" Name :"+name);

                }
            }
            break;
    }
}



}

1 个答案:

答案 0 :(得分:0)

//您需要获得以下权限才能阅读联系人

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

//在全局变量

中声明
 static final int PICK_CONTACT=1;

//将此代码放入Click事件

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

//将此代码放在OnCreate()

之外
 @Override
 public void onActivityResult(int reqCode, int resultCode, Intent data) {
 super.onActivityResult(reqCode, resultCode, data);

 switch (reqCode) {
 case (PICK_CONTACT) :
   if (resultCode == Activity.RESULT_OK) {
String cNumber=null;
     Uri contactData = data.getData();
     Cursor c =  getActivity().managedQuery(contactData, null, null, null, null);
     if (c.moveToFirst()) {


         String id =c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts._ID));


         String hasPhone =c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

           if (hasPhone.equalsIgnoreCase("1")) {
          Cursor phones = getActivity().getContentResolver().query( 
                       ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null, 
                       ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id, 
                       null, null);
             phones.moveToFirst();
              cNumber = phones.getString(phones.getColumnIndex("data1"));

           }
         String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
System.out.println("number is:"+cNumber+" Name :"+name);
Toast.makeText(getActivity(), "number is:"+cNumber+" Name :"+name, Toast.LENGTH_LONG).show();
     }
   }
   break;
 }
 }