从 Android 联系人中检索电话号码在三星上不起作用

Posted

技术标签:

【中文标题】从 Android 联系人中检索电话号码在三星上不起作用【英文标题】:Retrieving Phone Number from Android Contacts Doesn't Work On Samsung 【发布时间】:2014-05-25 02:03:00 【问题描述】:

当我将代码从"// Using the contact ID now we will get contact phone number" 行开始放入"cursorPhone.close" 行时,它不会在我的s3 或note 3 上显示任何联系信息,但会在我的华硕平板电脑上显示它。如果我在上面提到的行之间取出代码,该代码适用于 s3 和注释 3。我做错了什么?日志中没有错误。

private void getContacts() 

    try 

        String[] projection = new String[] 
                ContactsContract.Contacts.DISPLAY_NAME,
                ContactsContract.Contacts.HAS_PHONE_NUMBER,
                ContactsContract.Contacts._ID ;

        mCursor = managedQuery(ContactsContract.Contacts.CONTENT_URI,
                projection, ContactsContract.Contacts.HAS_PHONE_NUMBER + "=?", new String[]  "1" ,
                ContactsContract.Contacts.DISPLAY_NAME);

            String phoneNumber = '';
        while (mCursor.moveToNext()) 
            contact contact = new contact();

            String contactId = mCursor.getString(mCursor.getColumnIndex(ContactsContract.Contacts._ID));
            contact.setContactName(mCursor.getString(mCursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));

    // Using the contact ID now we will get contact phone number
            Cursor cursorPhone = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                    new String[]ContactsContract.CommonDataKinds.Phone.NUMBER,

                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ? AND " +
                            ContactsContract.CommonDataKinds.Phone.TYPE + " = " +
                            ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE,

                    new String[]contactId,
                    null);

            if (cursorPhone.moveToFirst()) 
                phoneNumber = (cursorPhone.getString(cursorPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
            

            cursorPhone.close();//till here             




            contact_list.add(contact);
        
        isChecked = new boolean[mCursor.getCount()];

        for (int i = 0; i < isChecked.length; i++) 
            isChecked[i] = false;
        

        this.mContactAdapter = new contactAdapter(this, R.layout.contactlistview, contact_list);
        lv.setAdapter(this.mContactAdapter);
        mCursor.close();

        runOnUiThread(returnRes);

     catch (Exception e) 
        Log.d("getContacts", e.getMessage());
    

【问题讨论】:

【参考方案1】:

你为什么不尝试使用 -

打开内置联系人活动
Intent intent = new Intent(Intent.ACTION_PICK,
        android.provider.ContactsContract.CommonDataKinds.Phone.CONTENT_URI);

startActivityForResult(intent, PICK_CONTACT); Hope this helps you.

你需要像这样在 Activity 的 Activity Result Method 中处理代码:

    if (reqCode == PICK_CONTACT && resultCode == Activity.RESULT_OK) 

            //  final String phoneNumber = data.getStringExtra("android.intent.extra.PHONE_NUMBER");

                if (data != null) 
                    Uri uri = data.getData();

                    if (uri != null) 
                        Cursor c = null;
                        try 
                            c = getContentResolver().query(uri, new String[]ContactsContract.CommonDataKinds.Phone.NUMBER,
                                    null, null, null);

                            if (c != null && c.moveToFirst()) 
                                String number = c.getString(0); 
                                number=number.replaceAll("\\s", "");
                                number=number.replaceAll("-", "");
                                edtPhoneNumber.setText(number);
                            
                         finally 
                            if (c != null) 
                                c.close();
                            
                        
                    
                


                finishActivity(PICK_CONTACT);
            
            if (reqCode == PICK_CALLLOG && resultCode == Activity.RESULT_OK) 
                edtPhoneNumber.setText(data.getStringExtra("CallerNumber"));

            
            if (reqCode == PICK_SMSLOG && resultCode == Activity.RESULT_OK) 
                edtPhoneNumber.setText(data.getStringExtra("CallerNumber"));
            

【讨论】:

1.因为我正在构建一个自定义列表视图 2. 还构建了一次选择多个联系人的能力。

以上是关于从 Android 联系人中检索电话号码在三星上不起作用的主要内容,如果未能解决你的问题,请参考以下文章

从android中的电话簿中检索whatsapp联系人

从 Android 联系人中获取电子邮件和电话

android获取联系人图片、姓名和手机号码

如何从 Android 设备中仅获取电话号码联系人

仅从 android 中检索电话号码包含 123 的联系人

在 Android 3.0 上检索电话联系人