我試圖根據聯繫人電話號碼檢索聯繫人姓名。我做了一個可以在所有API版本中工作的函數,因爲我無法在1.6版本中使其工作,並且我看不到問題,也許有人可以發現它?在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.6了! http://developer.android.com/resources/dashboard/platform-versions.html。它僅佔當前用戶羣的2.2%,這個數字將縮小,縮小,縮小。它可能永遠不會達到零,但這僅僅是因爲技術落後者無論如何都不會聽說你的新出血邊緣應用程序!不要浪費你的時間! –
對於其他人的設施,我寫了一篇文章,其中包含了整個代碼,用於查詢姓名,照片,聯繫人ID等,並給出了正確的解釋。該代碼包含在不同答案中發現的片段,但更多地組織和測試。鏈接:http://hellafun.weebly.com/home/get-information-of-a-contact-from-number – Usman