我想从一些我曾试图通过查询检索其联系人的姓名,但我没有得到result..It将返回本身的数量.....我已经保存该号码给联系人。麻烦检索联系人信息
我试过代码...
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String x = getContactNameFromNumber("+918281306132");
System.out.println(x);
}
private String getContactNameFromNumber(String number) {
// define the columns I want the query to return
System.out.println("Entering into getContactNameFromNumber");
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;
}
}
我还添加了读取手机状态的权限了。有人请帮我找回联系人的名字。
您的代码似乎确定。你可以尝试改变'字符串名称= c.getString(C .getColumnIndex(Contacts.Phones.DISPLAY_NAME));'到 '字符串名称= c.getString(0);' 。或者你可以试试我的[**片段**](http://pastie.org/6035954)。如果有效,我会添加答案。 –
我曾尝试更改我的代码仍然不工作。 – ammukuttylive
你在logcat中看到任何异常吗? –