获取使用Android的内容提供商联系号码 [英] Getting contact number using content provider in android

查看:84
本文介绍了获取使用Android的内容提供商联系号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我跟着这个教程,并得到了内容提供商的基础知识: HTTP:// WWW。 vogella.de/articles/AndroidSQLite/article.html

I followed this tutorial and got the basics of Content Providers : http://www.vogella.de/articles/AndroidSQLite/article.html

但想知道我怎样才能存储对显示名称的联络号码。试图用ContactsContract.Contacts.CONTENT_VCARD_TYPE。但得到了一个错误。

But wanted to know how can i get the contact number that is stored against display name. tried with "ContactsContract.Contacts.CONTENT_VCARD_TYPE". But got an error.

请让我知道是否有任何解决方案。

Please let me know if there is any solution.

由于结果
Sneha

Thanks
Sneha

推荐答案

这是有关使用android的内容提供商获取联系人号码一个很好的教程

This is a good tutorial about Getting contact number using content provider in android

http://www.higherpass.com/Android/Tutorials /工作与 - Android的联系人/

http://www.app-solut.com/blog/2012/06/retrieval-of-contacts-with-contact-contract/

和可以选择这样的联络号码

and can pick contact number like this

添加按钮单击事件像

button1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

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


//outside button click

public void onActivityResult(int reqCode, int resultCode, Intent data) {
        super.onActivityResult(reqCode, resultCode, data);

        switch (reqCode) {
        case (PICK_CONTACT):


            if (resultCode == Activity.RESULT_OK) {
                getContactInfo(data);
            }
        }
    }

    // onActivityResult

    private void getContactInfo(Intent data) {
        // TODO Auto-generated method stub

         ContentResolver cr = getContentResolver();




        Cursor cursor = managedQuery(data.getData(), null, null, null, null);
        while (cursor.moveToNext()) {
            String contactId = cursor.getString(cursor
                    .getColumnIndex(ContactsContract.Contacts._ID));
            Name = cursor
                    .getString(cursor
                            .getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));

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

            Cursor emailCur = cr.query( 
                    ContactsContract.CommonDataKinds.Email.CONTENT_URI, 
                    null,
                    ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", 
                    new String[]{contactId}, null); 


            emailCur.close();






     if (hasPhone.equalsIgnoreCase("1"))
                hasPhone = "true";
            else
                hasPhone = "false";

            if (Boolean.parseBoolean(hasPhone)) {
                Cursor phones = getContentResolver().query(
                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                        null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                + " = " + contactId, null, null);
                while (phones.moveToNext()) {
                    phoneNo = phones
                            .getString(phones
                                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                }
                phones.close();
            }

            pname.setText(Name);
            //
            phno.setText(phoneNo);

        Toast.makeText(this, Name + "   " + phoneNo, Toast.LENGTH_SHORT).show();


        }

    }

这篇关于获取使用Android的内容提供商联系号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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