所有 我想在我的应用程序数据库中从android的电话簿中取出数字.. 我用下面的代码尝试了它但是这里得到的人的名字而不是我想要电话簿中的号码并想将它存储在我的数据库中..如何实现这个????可以任何人指导我..
@Override
public void onActivityResult(int reqCode, int resultCode, Intent data){
super.onActivityResult(reqCode, resultCode, data);
switch(reqCode){
case (PICK_CONTACT):
if (resultCode == Activity.RESULT_OK){
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);
if (c.moveToFirst()){
// other data is available for the Contact. I have decided
// to only get the name of the Contact.
String name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.CONTENT_TYPE));
Toast.makeText(getApplicationContext(), name, Toast.LENGTH_SHORT).show();
}
}
}
提前致谢 -
答案 0 :(得分:6)
试试这段代码,
@Override public void onActivityResult(int reqCode, int resultCode, Intent data){ super.onActivityResult(reqCode, resultCode, data); switch(reqCode){ case (PICK_CONTACT): if (resultCode == Activity.RESULT_OK) { Uri contactData = data.getData(); Cursor c = managedQuery(contactData, null, null, null, null); if (c.moveToFirst()) { String id = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts._ID)); String hasPhone = c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)); if (hasPhone.equalsIgnoreCase("1")) { Cursor phones = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id, null, null); phones.moveToFirst(); String cNumber = phones.getString(phones.getColumnIndex("data1")); } } } }
答案 1 :(得分:6)
我知道这是一个老问题,但我认为来自 Android的以下资源对此事非常有帮助。 “奖金”部分是raj正在寻找的确切代码。我认为这个链接应该对将来看到这个问题的人有所帮助,特别是如果你不了解CapDroid的片段。
http://developer.android.com/training/basics/intents/result.html
答案 2 :(得分:0)
这将对您有所帮助:
public void onActivityResult(int reqCode, int resultCode, Intent data) { super.onActivityResult(reqCode, resultCode, data);
try {
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor cur = managedQuery(contactData, null, null, null, null);
ContentResolver contect_resolver = getContentResolver();
if (cur.moveToFirst()) {
String id = cur.getString(cur.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
String name = "";
String no = "";
Cursor phoneCur = contect_resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id}, null);
if (phoneCur.moveToFirst()) {
name = phoneCur.getString(phoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
no = phoneCur.getString(phoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
Log.e("Phone no & name :***: ", name + " : " + no);
txt.append(name + " : " + no + "\n");
id = null;
name = null;
no = null;
phoneCur = null;
}
contect_resolver = null;
cur = null;
// populateContacts();
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
Log.e("IllegalArgumentException::", e.toString());
} catch (Exception e) {
e.printStackTrace();
Log.e("Error :: ", e.toString());
}
}