Get contact name given a phone number in Android

I am trying to retrieve contact names given the contact phone number. I made a function that should work in all API versions, by I can't make it work in 1.6 and I can't see the problem, maybe someone can spot it? Note that, I've replaced the API constants for strings so I don't have deprecated warning problems. public String getContactName(final String phoneNumber) { Uri uri; String[] projection; if (Build.VERSION.SDK_INT >= 5) { uri = Uri.parse("content://"); 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; }
Don't support 1.6 anymore!…. It makes up for only 2.2% of the current user base and that number will shrink, shrink, shrink. It may never hit zero, but that's only because of technology laggards that aren't going to hear about your new bleeding edge app anyway! Don't waste your time!

以上就是Get contact name given a phone number in Android的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准