2011-07-17 12 views
10

連絡先の電話番号を指定して連絡先の名前を取得しようとしています。私は1.6で動作させることができないため、すべてのAPIバージョンで動作するはずの関数を作ったが、問題を見ることができないかもしれない。Androidで電話番号を指定して連絡先の名前を取得する

文字列のAPI定数を置き換えているため、推奨されない警告の問題はありません。

public String getContactName(final String phoneNumber) 
{ 
    Uri uri; 
    String[] projection; 

    if (Build.VERSION.SDK_INT >= 5) 
    { 
     uri = Uri.parse("content://com.android.contacts/phone_lookup"); 
     projection = new String[] { "display_name" }; 
    } 
    else 
    { 
     uri = Uri.parse("content://contacts/phones/filter"); 
     projection = new String[] { "name" }; 
    } 

    uri = Uri.withAppendedPath(uri, Uri.encode(phoneNumber)); 
    Cursor cursor = this.getContentResolver().query(uri, projection, null, null, null); 

    String contactName = ""; 

    if (cursor.moveToFirst()) 
    { 
     contactName = cursor.getString(0); 
    } 

    cursor.close(); 
    cursor = null; 

    return contactName; 
} 
+1

もう1.6をサポートしていません! http://developer.android.com/resources/dashboard/platform-versions.htmlそれは現在のユーザベースのわずか2.2%を占め、その数は縮小、縮小、縮小するでしょう。それはゼロになることは決してないかもしれませんが、それはあなたの新しい出血の端のアプリについては、とにかく聞くつもりはない技術の遅れのためだけです!あなたの時間を無駄にしないでください! –

+0

他の施設のために、名前、写真、連絡先IDなどを質問するためのコード全体がまともに書かれています。このコードにはさまざまな回答で見つかったスニペットが含まれていますが、より整理され、テストされています。リンク:http://hellafun.weebly.com/home/get-information-of-a-contact-from-number – Usman

答えて

9

sdkのバージョンを比較する代わりに反射を使用します。

public String getContactName(final String phoneNumber) 
{ 
    Uri uri; 
    String[] projection; 
    mBaseUri = Contacts.Phones.CONTENT_FILTER_URL; 
    projection = new String[] { android.provider.Contacts.People.NAME }; 
    try { 
     Class<?> c =Class.forName("android.provider.ContactsContract$PhoneLookup"); 
     mBaseUri = (Uri) c.getField("CONTENT_FILTER_URI").get(mBaseUri); 
     projection = new String[] { "display_name" }; 
    } 
    catch (Exception e) { 
    } 


    uri = Uri.withAppendedPath(mBaseUri, Uri.encode(phoneNumber)); 
    Cursor cursor = this.getContentResolver().query(uri, projection, null, null, null); 

    String contactName = ""; 

    if (cursor.moveToFirst()) 
    { 
     contactName = cursor.getString(0); 
    } 

    cursor.close(); 
    cursor = null; 

    return contactName; 
} 
+0

こんにちは、私は似たようなことを試みましたが、うまくいきませんでした。ここに私の質問です、私は本当にあなたの助けに感謝します! :) http://stackoverflow.com/questions/35097844/get-contact-name/35098111#35098111 –

19

これは、最新バージョンでは正常に動作するようです:

private String getContactName(Context context, String number) { 

    String name = null; 

    // define the columns I want the query to return 
    String[] projection = new String[] { 
      ContactsContract.PhoneLookup.DISPLAY_NAME, 
      ContactsContract.PhoneLookup._ID}; 

    // encode the phone number and build the filter URI 
    Uri contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number)); 

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

    if(cursor != null) { 
     if (cursor.moveToFirst()) { 
      name =  cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME)); 
      Log.v(TAG, "Started uploadcontactphoto: Contact Found @ " + number);    
      Log.v(TAG, "Started uploadcontactphoto: Contact name = " + name); 
     } else { 
      Log.v(TAG, "Contact Not Found @ " + number); 
     } 
     cursor.close(); 
    } 
    return name; 
} 
+1

ありがとう、それは動作しますが、多くの名前を取得する場合は、asyncTaskにする必要がありますので、メインスレッドに多くの負荷をかけます。 – Aziz

0
private String getContactNameFromNumber(String number) { 
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number)); 


Cursor cursor = context.getContentResolver().query(uri, new String[]{PhoneLookup.DISPLAY_NAME},null,null,null); 
if (cursor.moveToFirst()) 
{ 
    name = cursor.getString(cursor.getColumnIndex(PhoneLookup.D 
0
public static String getContactName(Context context, String phoneNumber) 
{ 
    ContentResolver cr = context.getContentResolver(); 
    Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber)); 
    Cursor cursor = cr.query(uri, new String[]{PhoneLookup.DISPLAY_NAME}, null, null, null); 
    if (cursor == null) 
    { 
     return null; 
    } 
    String contactName = null; 
    if(cursor.moveToFirst()) 
    { 
     contactName = cursor.getString(cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME)); 
    } 

    if(cursor != null && !cursor.isClosed()) { 
     cursor.close(); 
    } 

    return contactName; 
} 
関連する問題