web-dev-qa-db-ja.com

androidですべての連絡先の電話番号を読み取ります

このコードを使用して、すべての連絡先名と電話番号を取得しています。

String[] projection = new String[]
{
    People.NAME,
    People.NUMBER
};

Cursor c = ctx.getContentResolver().query(People.CONTENT_URI, projection, null, null, People.NAME + " ASC");
c.moveToFirst();

int nameCol = c.getColumnIndex(People.NAME);
int numCol = c.getColumnIndex(People.NUMBER);

int nContacts = c.getCount();

do
{
  // Do something
} while(c.moveToNext());

ただし、これは各連絡先のプライマリ番号のみを返しますが、セカンダリ番号も取得したいです。これどうやってするの?

68
shuwo

次の方法で、連絡先に関連付けられているすべての電話番号を読み取ることができます。

Uri personUri = ContentUris.withAppendedId(People.CONTENT_URI, personId);
Uri phonesUri = Uri.withAppendedPath(personUri, People.Phones.CONTENT_DIRECTORY);
String[] proj = new String[] {Phones._ID, Phones.TYPE, Phones.NUMBER, Phones.LABEL}
Cursor cursor = contentResolver.query(phonesUri, proj, null, null, null);

この例(あなたのものと同様)は非推奨の連絡先APIを使用していることに注意してください。 Eclair以降では、これは ContactsContract APIに置き換えられました。

29
Graeme Duncan

次のコードは、すべての電話番号と名前を読み取る簡単な方法を示しています。

Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
while (phones.moveToNext())
{
  String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
  String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

}
phones.close();

注:getContentResolverは、Activityコンテキストからのメソッドです。

147
Dinesh Kumar

このコードは、各連絡先のall電話番号を取得する方法を示しています。

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()) {
                  int phoneType = pCur.getInt(pCur.getColumnIndex(
                      ContactsContract.CommonDataKinds.Phone.TYPE));
                  String phoneNumber = pCur.getString(pCur.getColumnIndex(
                      ContactsContract.CommonDataKinds.Phone.NUMBER));
                  switch (phoneType) {
                        case Phone.TYPE_MOBILE:
                            Log.e(name + "(mobile number)", phoneNumber);
                            break;
                        case Phone.TYPE_HOME:
                            Log.e(name + "(home number)", phoneNumber);
                            break;
                        case Phone.TYPE_WORK:
                            Log.e(name + "(work number)", phoneNumber);
                            break;
                        case Phone.TYPE_OTHER:
                            Log.e(name + "(other number)", phoneNumber);
                            break;                                  
                        default:
                            break;
                  }
              } 
              pCur.close();
        }
    }
}
24
valerybodak

役立つ場合は、ContactsContract APIを使用して最初に名前で連絡先を見つけ、次に特定の番号タイプを探して詳細を反復処理する例を示します。

ContactsContractを使用して電話番号とメールアドレスを取得する方法

14
AndroidRef.com
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) 
{
    System.out.println("name : " + name + ", ID : " + id);

    // get the <span id="IL_AD4" class="IL_AD">phone
    // number</span>
    Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
                           null, 
                           ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id }, null);

    while (pCur.moveToNext())
    {
        String phone = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
        System.out.println("phone" + phone);
    }

pCur.close();
10
user1554037

これを使用して、すべての電話連絡先を取得できます。

    Cursor c = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
            new String[] { ContactsContract.Contacts._ID,
                    ContactsContract.Contacts.DISPLAY_NAME,
                    ContactsContract.CommonDataKinds.Phone.NUMBER,
                    ContactsContract.RawContacts.ACCOUNT_TYPE },
            ContactsContract.RawContacts.ACCOUNT_TYPE + " <> 'google' ",
            null, null);

完全な例を確認してください [〜#〜] here [〜#〜] ...........

9
Gagan Deep

回答は受け入れられましたが、一意の番号は与えられませんでした。

このコードを参照してください、それは一意の番号を返します。

public static void readContacts(Context context) {
        if (context == null)
            return;
        ContentResolver contentResolver = context.getContentResolver();

        if (contentResolver == null)
            return;

        String[] fieldListProjection = {
                ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
                ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                ContactsContract.CommonDataKinds.Phone.NUMBER,
                ContactsContract.CommonDataKinds.Phone.NORMALIZED_NUMBER,
                ContactsContract.Contacts.HAS_PHONE_NUMBER
        };

        Cursor phones = contentResolver
                .query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI
                        , fieldListProjection, null, null, null);
        HashSet<String> normalizedNumbersAlreadyFound = new HashSet<>();

        if (phones != null && phones.getCount() > 0) {
            while (phones.moveToNext()) {
                String normalizedNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NORMALIZED_NUMBER));
                if (Integer.parseInt(phones.getString(phones.getColumnIndex(
                        ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                    if (normalizedNumbersAlreadyFound.add(normalizedNumber)) {
                        String id = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
                        String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                        String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        Log.d("test", " Print all values");
                    }
                }
            }
            phones.close();
        }
    }
1
Yogesh Rathi

同様に、他の「People」参照を使用して他の番号を取得するだけです

People.TYPE_HOME
People.TYPE_MOBILE
People.TYPE_OTHER
People.TYPE_WORK
1
Donal Rafferty

これ one が最終的に私が探していた答えをくれました。電話の連絡先のすべての名前と電話番号を取得できます。

package com.test;

import Android.app.Activity;
import Android.content.ContentResolver;
import Android.database.Cursor;
import Android.os.Bundle;
import Android.provider.ContactsContract;

public class TestContacts extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ContentResolver cr = getContentResolver();
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
                              null, null, null, null);
        if (cur != null && cur.getCount() > 0) {
            while (cur.moveToNext()) {

                if (Integer.parseInt.equals(cur.getString(cur.getColumnIndex(
                            ContactsContract.Contacts.HAS_PHONE_NUMBER)))) {
                    String id = cur.getString(cur.getColumnIndex(
                                ContactsContract.Contacts._ID));
                    String name = cur.getString(cur.getColumnIndex(
                                ContactsContract.Contacts.DISPLAY_NAME));
                    Cursor pCur = cr.query(
                                           ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                            null,
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                    + " = ?", new String[] { id }, null);
                    int i = 0;
                    int pCount = pCur.getCount();
                    String[] phoneNum = new String[pCount];
                    String[] phoneType = new String[pCount];
                    while (pCur != null && pCur.moveToNext()) {
                        phoneNum[i] = pCur.getString(pCur.getColumnIndex(
                        ContactsContract.CommonDataKinds.Phone.NUMBER));
                        phoneType[i] = pCur.getString(pCur.getColumnIndex(
                                ContactsContract.CommonDataKinds.Phone.TYPE));
                        i++;
                    }
                }

            }

        }

    }
}
1
craned

連絡先の名前(昇順)と番号を取得するコード

参考: リストビューで連絡先を表示

Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME+" ASC");
        while (phones.moveToNext())
        {
            String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));


        }
        phones.close();
0
Parsania Hardik

私は私の愛する必要を修正しました

マニフェスト許可

        <uses-permission Android:name="Android.permission.READ_CONTACTS" />
        <uses-permission Android:name="Android.permission.WRITE_CONTACTS"/>
        <!-- Read Contacts from phone -->
        <uses-permission Android:name="Android.permission.read_contacts" />
        <uses-permission Android:name="Android.permission.read_phone_state" />
        <uses-permission Android:name="Android.permission.READ_CALL_LOG" />

注:マニフェストで許可を与えた後、一部の携帯電話では拒否することができます。設定->アプリケーション->アプリケーションを見つけてクリックし、必要な許可をオンにします

enter image description here

 btn_readallcontacks.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
            List<User> userList = new ArrayList<User>();
            while (phones.moveToNext()) {
                User user = new User();
                user.setNamee(phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)));
                user.setPhone(phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
                userList.add(user);
                Log.d(TAG, "Name : " + user.getNamee().toString());
                Log.d(TAG, "Phone : " + user.getPhone().toString());
            }
            phones.close();
        }
    });
0
Sam

CursorLoaderを使用して連絡先をバックグラウンドで読み込みます。

  CursorLoader cursor = new CursorLoader(this, ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
            Cursor managedCursor = cursor.loadInBackground();

            int number = managedCursor.getColumnIndex(ContactsContract.Contacts.Data.DATA1);
            int name = managedCursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
            int index = 0;
            while (managedCursor.moveToNext()) {
                String phNumber = managedCursor.getString(number);
                String phName = managedCursor.getString(name);
            }
0
AskQ