Java 我想获取联系人姓名和号码,但我的代码只是打印联系人姓名,所以我如何才能同时获取号码
我的目标是获得联系人姓名和号码,但我的代码只是打印姓名,这样我就可以获得联系人号码。虽然我的代码按姓名获取联系人,但当我单击特定姓名时,它只显示姓名。请编辑我的代码,它也会得到名字和号码。提前感谢您获取电话号码Java 我想获取联系人姓名和号码,但我的代码只是打印联系人姓名,所以我如何才能同时获取号码,java,android,Java,Android,我的目标是获得联系人姓名和号码,但我的代码只是打印姓名,这样我就可以获得联系人号码。虽然我的代码按姓名获取联系人,但当我单击特定姓名时,它只显示姓名。请编辑我的代码,它也会得到名字和号码。提前感谢您获取电话号码 public class SearchViewActivity extends Activity { private TextView resultText; @Override protected void onCreate(Bundle savedInst
public class SearchViewActivity extends Activity {
private TextView resultText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_view);
resultText = (TextView)findViewById(R.id.searchViewResult);
setupSearchView();
}
private void setupSearchView() {
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
final SearchView searchView = (SearchView) findViewById(R.id.searchView);
SearchableInfo searchableInfo = searchManager.getSearchableInfo(getComponentName());
searchView.setSearchableInfo(searchableInfo);
}
@Override
protected void onNewIntent(Intent intent) {
if (ContactsContract.Intents.SEARCH_SUGGESTION_CLICKED.equals(intent.getAction())) {
//handles suggestion clicked query
String displayName = getDisplayNameForContact(intent);
resultText.setText(displayName);
} else if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
// handles a search query
String query = intent.getStringExtra(SearchManager.QUERY);
resultText.setText("should search for query: '" + query + "'...");
}
}
private String getDisplayNameForContact(Intent intent) {
Cursor phoneCursor = getContentResolver().query(intent.getData(), null, null, null, null);
phoneCursor.moveToFirst();
int idDisplayName = phoneCursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
String name = phoneCursor.getString(idDisplayName);
phoneCursor.close();
return name;
}
}
试试这个代码
Cursor phones = getContentResolver()
.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
null, null, null);
while (phones.moveToNext()) {
phoneNumber = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
所以我知道这有点晚了,但我遇到了同样的问题,并像这样修复了它
phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
private String getDisplayPhoneForContact(Intent intent){
Cursor phoneCursor = getContentResolver().query(intent.getData(), null,null,null,null);
String phoneNum= "0";
phoneCursor.moveToFirst();
String id = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.Contacts._ID));
Cursor pCur= getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=?", new String[]{id},null);
while(pCur.moveToNext()){
phoneNum = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
return phoneNum;
}