0

編集テキストとボタンがあるとします。編集テキストに番号を入力し、ボタンを押すと、連絡先情報が表示されるか、その連絡先の名前が返されます。

運が悪かったので、いろいろな方法を試しました。私が一番遠くまで成功したのは次のとおりです...しかし、名前を返すことができませんでした。

Cursor phoneCursor = null;
    contactList = new HashMap<String,String>();

  try{
       Uri uContactsUri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;

      String strProjection = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME;

      phoneCursor = getContentResolver().query(uContactsUri, null, null, null, strProjection);
      phoneCursor.moveToFirst();

      String name = "";
      String phoneNumber = "";

       int nameColumn = phoneCursor.getColumnIndex(Phone.DISPLAY_NAME);
       int phoneColumn = phoneCursor.getColumnIndex(Phone.NUMBER);



           phoneCursor.moveToNext();

        }
    }
    catch(Exception e){
        Log.e("[SmsMain] getContactData", e.toString());
    }
    finally{
       if(phoneCursor != null){
          phoneCursor.close();
          phoneCursor = null;
       }
    }
}
4

2 に答える 2

0

そのためには、ここで説明するように最適化されたPhoneLookupプロバイダーを使用する必要があります

Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI、Uri.encode(phoneNumber)); カーソルcursor=resolver.query(uri、new String [] {PhoneLookup.DISPLAY_NAME}、null、null、null);

詳細については、このリンクを参照してください

于 2012-04-07T17:36:12.633 に答える
0

以下のメソッドを使用して連絡先の名前を取得します。名前の代わりに番号が存在しない場合は、同じ番号が返されます。

private String getContactNameFromNumber(String number) {
        // define the columns I want the query to return
        String[] projection = new String[] {
                Contacts.Phones.DISPLAY_NAME,
                Contacts.Phones.NUMBER };

        // encode the phone number and build the filter URI
        Uri contactUri = Uri.withAppendedPath(Contacts.Phones.CONTENT_FILTER_URL, Uri.encode(number));

        // query time
        Cursor c = getContentResolver().query(contactUri, projection, null,
                null, null);

        // if the query returns 1 or more results
        // return the first result
        if (c.moveToFirst()) {
            String name = c.getString(c
                    .getColumnIndex(Contacts.Phones.DISPLAY_NAME));
            return name;
        }

        // return the original number if no match was found
        return number;
    }
于 2012-04-07T17:48:16.277 に答える