[英]How to pick contact number from phone-book of android in to my application?
all i want to take number from phonebook of android in my application database.. i have tried it with below code but here am getting name of person instead i want number from phonebook and want to store it in my database..how to achieve this????can any one guide me.. 所有我想从我的应用程序数据库中的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();
}
}
}
Thanks in Advance-- 提前致谢 -
I know that this is an old question, but I think the below resource from Android is extremely helpful on this matter. 我知道这是一个老问题,但我认为在 Android下面的资源是在这个问题上非常有帮助。 The "bonus" section is the exact code that raj was looking for.
“奖金”部分是raj正在寻找的确切代码。 I think this link should be helpful to anyone who sees this question in the future, especially if you don't understand CapDroid's snippet.
我认为这个链接应该对将来看到这个问题的人有所帮助,特别是如果你不了解CapDroid的片段。
http://developer.android.com/training/basics/intents/result.html http://developer.android.com/training/basics/intents/result.html
try this code, 试试这段代码,
@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")); } } } }
This will help you: 这将有助于您:
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());
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.