获得从通讯录Android的一个电话号码 [英] Get a single phone number from contacts Android

查看:109
本文介绍了获得从通讯录Android的一个电话号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我想获得从联系人列表一个电话号码。我发现code这让我整个联系人列表的电话号码,我要的是该项目的电话号码点击。任何帮助将感激AP preciated。谢谢..

 公共无效的onClick(视图v){
意图contactPickerIntent =新意图(Intent.ACTION_PICK,Contacts.CONTENT_URI);
startActivityForResult(contactPickerIntent,CONTACT_PICKER_RESULT);ContentResolver的CR = getActivity()getContentResolver()。
光标CUR = cr.query(ContactsContract.Contacts.CONTENT_URI,NULL,NULL,NULL,NULL);    如果(cur.getCount()大于0){
        而(cur.moveToNext()){
            字符串ID = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
            字符串名称= cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));            如果(的Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)))大于0){
                光标pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                空,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +=?,新的String [] {ID},NULL);                而(pCur.moveToNext()){
                    串PHONENO = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    Toast.makeText(getActivity(),PHONENO,Toast.LENGTH_SHORT).show();
                }
            }
        }
    }
}
});


解决方案

当你在你的联系人列表中,点击联系人,你会得到你的 Intent.ACTION_PICK 意图。

因此​​,要处理的结果,你必须重写的onActivityResult()方法。

您收到意图对象由的onActivityResult返回的数据联系人的查找URI()

所以,你需要做的是这样的:

  @覆盖
保护无效的onActivityResult(INT申请code,INT结果code,意图数据){    如果(要求code == CONTACT_PICKER_RESULT){
        如果(结果code == Activity.RESULT_OK){            URI URI = data.getData();
            ContentResolver的ContentResolver的= getContentResolver();
            光标contentCursor = contentResolver.query(URI,NULL,NULL,NULL,NULL);            如果(contentCursor.moveToFirst()){
                字符串ID = contentCursor.getString(contentCursor.getColumnIndexOrThrow(ContactsContract.Contacts._ID));                字符串hasPhone =
                        contentCursor.getString(contentCursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));                如果(hasPhone.equalsIgnoreCase(1))
                {
                    光标手机= getContentResolver()查询(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,空,
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID +=+ ID,NULL,NULL);
                    phones.moveToFirst();
                    串contactNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    Log.i(PHONENUMBER,电话号码是+ contactNumber);                }
            }
            super.onActivityResult(要求code,结果code,数据);
        }
    }
}

然后你只需做任何你想要的值 contactNumber

希望有帮助你。

Hi I am trying to get a single phone number from the contact list . I have found code which gets me the whole contact list telephone numbers, all I want is the telephone number for the item clicked. Any Help would be gratefully appreciated . Thanks ..

public void onClick(View v) {
Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,Contacts.CONTENT_URI);
startActivityForResult(contactPickerIntent,CONTACT_PICKER_RESULT);

ContentResolver cr = getActivity().getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null,null, null);

    if (cur.getCount() > 0) {
        while (cur.moveToNext()) {
            String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
            String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

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

                while (pCur.moveToNext()) {
                    String phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    Toast.makeText(getActivity(), phoneNo,Toast.LENGTH_SHORT).show();
                }
            }
        }
    }
}
});

解决方案

When you are in your contacts list and click a Contact, you are going to get the Result of your Intent.ACTION_PICK intent.

So, to handle the result, you have to override onActivityResult() method.

You receive the lookup URI of your contact in the data Intent object returned by onActivityResult().

So, all you have to do is this:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if(requestCode == CONTACT_PICKER_RESULT){
        if(resultCode==Activity.RESULT_OK){

            Uri uri = data.getData();
            ContentResolver contentResolver = getContentResolver();
            Cursor contentCursor = contentResolver.query(uri, null, null,null, null);

            if(contentCursor.moveToFirst()){
                String id = contentCursor.getString(contentCursor.getColumnIndexOrThrow(ContactsContract.Contacts._ID));

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

                if (hasPhone.equalsIgnoreCase("1")) 
                {
                    Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null, 
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id,null, null);
                    phones.moveToFirst();
                    String contactNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    Log.i("phoneNUmber", "The phone number is "+ contactNumber);

                }
            }
            super.onActivityResult(requestCode, resultCode, data);
        }
    }
}

Then you just have to do whatever you want with the value on contactNumber.

Hope to have help you.

这篇关于获得从通讯录Android的一个电话号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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