如何使用 android studio 从联系人列表中选择联系人号码?

Posted

技术标签:

【中文标题】如何使用 android studio 从联系人列表中选择联系人号码?【英文标题】:How to select contact number from contact list using android studio? 【发布时间】:2016-03-01 11:27:14 【问题描述】:

Look at my screenshot

如何从联系人列表中获取所选联系人的号码和姓名。

【问题讨论】:

你试过什么?> 看我的截图 你的代码在哪里?截图是不够的。 下面是我的源代码 【参考方案1】:

点击代码

 Intent pickContact = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
 pickContact.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE)
 startActivityForResult(pickContact, 1);

onActivityResult 代码

 Uri contactData = data.getData();
 Cursor c = getContentResolver().query(contactData, null, null, null, null);
 if (c.moveToFirst()) 
                   int phoneIndex = getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
                   String num = c.getString(phoneIndex);
                   Toast.makeText(MainActivity.this, "Number=" + num, Toast.LENGTH_LONG).show();
           

【讨论】:

【参考方案2】:

要从您的联系人列表中获取选定联系人的联系人姓名、电话号码以及电子邮件,您只需添加此代码即可。

private Button btn;
private String TAG = "Contacts";
private static final int RESULT_PICK_CONTACT = 1;


btn.setOnClickListener(new View.OnClickListener()     
@Override
public void onClick(View v) 
selectSingleContact();
    
);

private void selectSingleContact()    
Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
startActivityForResult(contactPickerIntent, RESULT_PICK_CONTACT);
    

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) 
// check whether the result is ok
if (resultCode == RESULT_OK) 
    // Check for the request code, we might be usign multiple startActivityForResult
    switch (requestCode) 
        case RESULT_PICK_CONTACT:
            contactPicked(data);
            break;
    
 else 
    Log.e("ContactFragment", "Failed to pick contact");
       


private void contactPicked(Intent data) 

Uri uri = data.getData();
Log.i(TAG, "contactPicked() uri " + uri.toString());
Cursor cursor;
ContentResolver cr = getActivity().getContentResolver();

try 
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
    if (null != cur && cur.getCount() > 0) 
        cur.moveToFirst();
        for (String column : cur.getColumnNames()) 
            Log.i(TAG, "contactPicked() Contacts column " + column + " : " + cur.getString(cur.getColumnIndex(column)));
        
    

    if (cur.getCount() > 0) 
      //Query the content uri
        cursor = getActivity().getContentResolver().query(uri, null, null, null, null);

        if (null != cursor && cursor.getCount() > 0) 
            cursor.moveToFirst();
            for (String column : cursor.getColumnNames()) 
                Log.i(TAG, "contactPicked() uri column " + column + " : " + cursor.getString(cursor.getColumnIndex(column)));
            
        

        cursor.moveToFirst();
        id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
        Log.i(TAG, "contactPicked() uri id " + id);
        String contact_id = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
        Log.i(TAG, "contactPicked() uri contact id " + contact_id);
        // column index of the contact name
        name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
        // column index of the phone number
        phoneNo = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
        //get Email id of selected contact....
        Log.e("ContactsFragment", "::>> " + id + name + phoneNo);

        Cursor cur1 = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
                ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", new String[]contact_id, null);

        if (null != cur1 && cur1.getCount() > 0) 
            cur1.moveToFirst();
            for (String column : cur1.getColumnNames()) 
                Log.i(TAG, "contactPicked() Email column " + column + " : " + cur1.getString(cur1.getColumnIndex(column)));
                email = cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
            

           //HERE YOU GET name, phoneno & email of selected contact from contactlist.....
            Log.e("setcontactDetails","::>>" + name+"\nPhoneno:" + phoneNo+"\nEmail: " + email);
         else 
            Log.e("setcontactDetails","::>>" + name+"\nPhoneno:" + phoneNo+"\nEmail: " + email);
        
    
 catch (IndexOutOfBoundsException e) 
    e.printStackTrace();
    

同时为您的 androidManifest.xml 添加权限 它对我有用,希望它也对你有用。

【讨论】:

这是非常低效的。 moveToFirst 返回一个布尔值,如果为 null,则为 false。无需继续检查游标并发出无关查询。【参考方案3】:
@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_usercontact);

    class MainActivity extends Activity 
        private final int REQUEST_CODE=99;
        protected void onCreate(Bundle savedInstanceState) 
            super.onCreate(savedInstanceState);
            setContentView(R.layout.content_usercontact);
            EditText editText = (EditText)findViewById(R.id.editTextcontacts1);
            EditText editText1 = (EditText)findViewById(R.id.editTextcontacts2);
            EditText editText2 = (EditText)findViewById(R.id.editTextcontacts3);
            editText.setOnClickListener(new View.OnClickListener() 
                @Override
                public void onClick(View v) 
                    Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
                    startActivityForResult(intent, REQUEST_CODE);
                
            );

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

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

        


        @Override
        public void onActivityResult(int reqCode, int resultCode, Intent data) 
            super.onActivityResult(reqCode, resultCode, data);
            switch (reqCode) 
                case (REQUEST_CODE):
                    if (resultCode == Activity.RESULT_OK) 
                        Uri contactData = data.getData();
                        Cursor c = getContentResolver().query(contactData, null, null, null, null);
                        if (c.moveToFirst()) 
                            String contactId = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));
                            String hasNumber = c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
                            String num = "";
                            if (Integer.valueOf(hasNumber) == 1) 
                                Cursor numbers = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);
                                while (numbers.moveToNext()) 
                                    num = numbers.getString(numbers.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                                    Toast.makeText(MainActivity.this, "Number=" + num, Toast.LENGTH_LONG).show();
                                
                            
                        
                        break;
                    
            
        

    

【讨论】:

这是作为答案发布的,但它不会尝试回答问题。它可能应该是编辑、评论、另一个问题或完全删除。

以上是关于如何使用 android studio 从联系人列表中选择联系人号码?的主要内容,如果未能解决你的问题,请参考以下文章

如何从表1中检索列数据并使用SQLite数据库将该列值插入到Android Studio的表2中?

如何在 Android Studios 中更新联系人的电话号码

如何使用 phonegap 作为复选框列表显示从 android 设备获取的联系人?

如何在android studio中显示按地址排序的联系人姓名和号码

联系人选择器未在 Android Studio 中提供预期结果

如何修复 Android Studio 中的“使用 JsonReader.setLenient(true) 在第 1 行第 1 列路径 $ 处接受格式错误的 JSON”错误