检索联系​​人的手机号码 [英] Retrieve mobile number from contact

查看:188
本文介绍了检索联系​​人的手机号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检索联系人(我想发送SMS)的移动电话号码。在这里,我的code:

I want to retrieve the mobile number from the contacts (I want to send an sms). Here my code:

//Selecting the contact
        Button buttonPickContact = (Button)findViewById(R.id.pickcontact);
        buttonPickContact.setOnClickListener(new Button.OnClickListener(){

            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
                startActivityForResult(intent, RQS_PICK_CONTACT);
            }});


@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);

        Cursor cursor = null;
        mName.setText(context.getString(R.string.not_available));
        mNumber.setText(context.getString(R.string.not_available));

        if(requestCode == RQS_PICK_CONTACT && resultCode == RESULT_OK && data != null){
            Log.d(TAG, "requestCode, resultCode, data ok"); 
            Uri uri = data.getData(); 
            try{
                String[] projection = new String[] {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.HAS_PHONE_NUMBER};
//              cursor =  getContentResolver().query(uri, projection, null, null, null);
                cursor =  getContentResolver().query(uri, null, null, null, null);
                cursor.moveToFirst();
                Log.d(TAG, "Trying to retrieve the name and the number"); 
                String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                String hasNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.HAS_PHONE_NUMBER)); 

                Log.d(TAG, "hasNumber "+hasNumber); 
                mName.setText(name);

                if(hasNumber.trim().equals("1")){
                    Log.d(TAG, "contact has telephone number"); 
                    //set name and number
                    String phoneNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    mNumber.setText(phoneNumber);
                }

            }catch(Exception ex){
                CharSequence text = context.getString(R.string.cannot_choose_contact); 
                Toast toast = Toast.makeText(context, text, duration);
                toast.show();
            }
            if(cursor!= null && !cursor.isClosed()){
                cursor.close(); 
            }
        }else{
            CharSequence text = context.getString(R.string.cannot_choose_contact); 
            Toast toast = Toast.makeText(context, text, duration);
            toast.show();
        }
    }

我得到:无法读取0行,列-1从CursorWindow ...

I am getting: Failed to read row 0, column -1 from CursorWindow...

我如何获得的电话号码 - ?我试图从右侧立柱检索

How do I get the phone number - am I trying to retrieve it from the right column?

预先感谢您的回答,

推荐答案

联系人的详细数据包含在从主触头本身就是一个单独的表(见的联系人API指南详细)。既然你发送短信,这可能是更为有用只得到谁拥有相关的电话号码联系人,所以你还不如直行包含电话号码表。对于URI,我使用:

The detailed data for a contact is contained in a separate table from the main contact itself (see the Contacts API guide for more detail). Since you're sending an SMS, it might be more useful to only get the contacts who have a phone number associated, so you might as well go straight for the table which contains phone numbers. For the URI, I use:

CommonDataKinds.Phone.CONTENT_URI

然后,你不必担心 HAS_PHONE_NUMBER 。一目了然,你的code的其余部分看起来正确或非常接近。如果你想继续下行的原始路径,你必须做这个表一个单独的查询无论如何,但它提供了你最初发现联系人的ID。

Then you don't have to worry about HAS_PHONE_NUMBER. At a glance, the rest of your code looks right or very close. If you wanted to continue down your original path, you'd have to do a separate query on this table anyway, but provide it the ID of the contact that you initially found.

这篇关于检索联系​​人的手机号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆