查询联系人的所有电话号码(解决) [英] Querying all phone numbers of a contact (SOLVED)
问题描述
我设法编写补充程序,当用户点击一个按钮,他将能够检索一个电话号码,将填补一个EditText框。的问题是,如果一个接触有多个号码,它将始终以最顶部的数目
我一直在读另一个线程,从联系人获取号码选取器,有一个答案,但我不明白这一点。由于我是新来的Android程序, 我将AP preciate如果有任何人可以给我一步一步的说明
更新
在尝试,我发现这工作。唯一的问题是,我如何与电话号码显示的电话类型
公共类测试扩展活动{
/ **第一次创建活动时调用。 * /
私有静态最终诠释CONTACT_PICKER_RESULT = 1001;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
按钮浏览=(按钮)findViewById(R.id.do_email_picker);
browse.setOnClickListener(新View.OnClickListener(){
公共无效的onClick(视图v){
// TODO自动生成方法存根
意图contactPickerIntent =新的意图(Intent.ACTION_PICK,Contacts.CONTENT_URI);
startActivityForResult(contactPickerIntent,CONTACT_PICKER_RESULT);
}
});
}
@覆盖
保护无效onActivityResult(INT申请code,INT结果code,意图数据){
// TODO自动生成方法存根
super.onActivityResult(要求code,因此code,数据);
如果(结果code == RESULT_OK){
开关(要求code){
案例CONTACT_PICKER_RESULT:
//把手接触结果
最后的EditText smsEntry =(EditText上)findViewById(R.id.sms_to);
光标光标= NULL;
字符串PHONENO =;
名单<字符串> allNumbers =新的ArrayList<字符串>();
INT phoneIdx = 0;
尝试{
开放的结果= data.getData();
Log.v(Akiff,有一个接触的结果:+ result.toString());
//从URI获得接触式ID
字符串ID = result.getLastPathSegment();
//查询一切手机
光标= getContentResolver()查询(Phone.CONTENT_URI,空,Phone.CONTACT_ID +=?,新的String [] {ID},空)。
phoneIdx = cursor.getColumnIndex(Phone.DATA);
//就让我们每个电话号码
如果(cursor.moveToFirst()){
而(cursor.isAfterLast()== FALSE){
PHONENO = cursor.getString(phoneIdx);
allNumbers.add(PHONENO);
cursor.moveToNext();
}
}其他{
Log.w(AKIFF,无号);
Toast.makeText(Test.this,这种接触没有号码,Toast.LENGTH_LONG).show();
}
}赶上(例外五){
Log.e(AKIFF,错误,E);
}最后{
如果(光标!= NULL){
cursor.close();
}
最终的String []项目= allNumbers.toArray(新的String [allNumbers.size());
AlertDialog.Builder建设者=新AlertDialog.Builder(Test.this);
builder.setTitle(选择一个数字:);
builder.setItems(项目,新DialogInterface.OnClickListener(){
公共无效的onClick(DialogInterface对话,诠释项){
// TODO自动生成方法存根
字符串selectedNumber =项目[项目]的ToString();
selectedNumber = selectedNumber.replace( - ,);
// selectedNumber = selectedNumber.replace(+,);
smsEntry.setText(selectedNumber);
}
});
AlertDialog警报= builder.create();
如果(allNumbers.size()→1){
alert.show();
}其他{
串selectedNumber = phoneNo.toString();
selectedNumber = selectedNumber.replace( - ,);
smsEntry.setText(selectedNumber);
}
如果(phoneNo.length()== 0){
Log.d(Akiff,不接触);
}
}
打破;
}
}其他{
//优雅地处理故障
Log.w(Akiff,警告行为不好吗?);
}
}
首先,你可能需要查询在电话簿中的所有联系人。
//运行查询
开放的我们的uri = ContactsContract.Contacts.CONTENT_URI;
的String []投影=新的String [] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME
};
串选择= ContactsContract.Contacts.IN_VISIBLE_GROUP +='1';
的String [] selectionArgs两个= NULL;
字符串排序顺序= ContactsContract.Contacts.DISPLAY_NAME +分页中局部ASC;
//构建适配器的联系人条目
光标mCursor = managedQuery(URI,投影,选择,selectionArgs两个,排序顺序);
//
//绑定mCursor到你的列表视图
//
之后,当用户选择在列表中查看联系人,你做第二次查询来获取标签的联系人和电话号码。
@覆盖
保护无效onListItemClick(ListView的L,视图V,INT位置,长的id){
mCursor.moveToPosition(位置);
startManagingCursor(mCursor);
字符串的ContactID = mCursor.getString(mCursor.getColumnIndex(ContactsContract.Contacts._ID));
光标phoneNumCursor = getContentResolver()查询(Phone.CONTENT_URI,
空,Phone.CONTACT_ID +=?,新的String [] {}的ContactID,NULL);
phoneNumCursor.moveToFirst();
矢量<串GT; phoneTypeList =新矢量<串GT;();
矢量<串GT; phoneNumberList =新矢量<串GT;();
而(!phoneNumCursor.isAfterLast()){
整型= phoneNumCursor.getInt(phoneNumCursor.getColumnIndex(Phone.TYPE));
phoneTypeList.add(将String.valueOf(Phone.getTypeLabel(getResources(),类型,)));
phoneNumberList.add(phoneNumCursor.getString(phoneNumCursor.getColumnIndex(Phone.NUMBER)));
phoneNumCursor.moveToNext();
}
//
//随意表明接触的标签和电话号码。 ^^
//
更新:
下面是一个例子,如果你想使用联系人选择器。
私有静态最终诠释CONTACT_PICKER_RESULT = 1001;
保护无效startContactPicker(){
意图contactPickerIntent =新的意图(Intent.ACTION_PICK,Contacts.CONTENT_URI);
startActivityForResult(contactPickerIntent,CONTACT_PICKER_RESULT);
}
@覆盖
保护无效onActivityResult(INT申请code,INT结果code,意图数据){
如果(结果code == RESULT_OK){
开关(要求code){
案例CONTACT_PICKER_RESULT:
光标光标= NULL;
字符串phoneNumber的=;
尝试 {
开放的结果= data.getData();
字符串ID = result.getLastPathSegment();
光标= getContentResolver()查询(Phone.CONTENT_URI,
空,Phone.CONTACT_ID +=?,新的String [] {ID},NULL);
INT phoneIdx = cursor.getColumnIndex(Phone.DATA);
如果(cursor.moveToFirst()){
而(!cursor.isAfterLast()){
phoneNumber的= cursor.getString(phoneIdx);
//
//这将经过选择的联系人的所有phoneNumber的。
//
cursor.moveToNext();
}
}
}赶上(例外五){
} 最后 {
如果(光标!= NULL){
cursor.close();
}
numberView.setText(phoneNumber的);
}
打破;
}
}
}
i managed to write add program that upon the user click a button, he will be able to retrieve a phone number that will fill an editText box. The problem is that if a contact has multiple numbers, it will always take the top most number.
I have been reading another thread , Getting Number from Contacts Picker, there is an answer, but i dont get it. As I am new to android programming, I'll appreciate if anyone out there could give me step by step instructions
Update
After trying , i found that this works. The only problem is , how do i display the phone type together with the phone number
public class Test extends Activity {
/** Called when the activity is first created. */
private static final int CONTACT_PICKER_RESULT=1001 ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button browse = (Button) findViewById(R.id.do_email_picker);
browse.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent contactPickerIntent= new Intent (Intent.ACTION_PICK,Contacts.CONTENT_URI);
startActivityForResult(contactPickerIntent,CONTACT_PICKER_RESULT);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==RESULT_OK){
switch(requestCode){
case CONTACT_PICKER_RESULT:
//handle contact results
final EditText smsEntry = (EditText) findViewById(R.id.sms_to);
Cursor cursor = null;
String phoneNo = " ";
List<String> allNumbers = new ArrayList<String>();
int phoneIdx = 0;
try{
Uri result = data.getData();
Log.v("Akiff", "Got a contact result: "+ result.toString());
//get the contact id from the Uri
String id = result.getLastPathSegment();
//query for everything phone
cursor= getContentResolver().query(Phone.CONTENT_URI, null, Phone.CONTACT_ID + "=?", new String []{id} , null);
phoneIdx=cursor.getColumnIndex(Phone.DATA);
//let's just get every phone number
if(cursor.moveToFirst()){
while(cursor.isAfterLast()==false){
phoneNo =cursor.getString(phoneIdx);
allNumbers.add(phoneNo);
cursor.moveToNext();
}
}else{
Log.w("AKIFF", "No number");
Toast.makeText(Test.this, "This contact has no number", Toast.LENGTH_LONG).show();
}
}catch(Exception e){
Log.e("AKIFF", "error ",e);
}finally{
if(cursor!=null){
cursor.close();
}
final String [] items = allNumbers.toArray(new String[allNumbers.size() ]) ;
AlertDialog.Builder builder = new AlertDialog.Builder(Test.this);
builder.setTitle("Choose a number: ");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
// TODO Auto-generated method stub
String selectedNumber = items[item].toString();
selectedNumber = selectedNumber.replace("-","");
//selectedNumber = selectedNumber.replace("+","");
smsEntry.setText(selectedNumber);
}
});
AlertDialog alert = builder.create();
if(allNumbers.size()>1){
alert.show();
}else{
String selectedNumber= phoneNo.toString();
selectedNumber=selectedNumber.replace("-", "");
smsEntry.setText(selectedNumber);
}
if(phoneNo.length()==0){
Log.d("Akiff", "No contact");
}
}
break;
}
}else{
//gracefully handle failure
Log.w("Akiff","Warning activity not okay");
}
}
First, you may need to query all contact in phone book.
// Run query
Uri uri = ContactsContract.Contacts.CONTENT_URI;
String[] projection = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME
};
String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '1'";
String[] selectionArgs = null;
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
// Build adapter with contact entries
Cursor mCursor = managedQuery(uri, projection, selection, selectionArgs, sortOrder);
//
// Bind mCursor to to your Listview
//
After that, when user select a contact in your list view, you make a second query to get label and number of that contact.
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
mCursor.moveToPosition(position);
startManagingCursor(mCursor);
String contactID = mCursor.getString(mCursor.getColumnIndex(ContactsContract.Contacts._ID));
Cursor phoneNumCursor = getContentResolver().query(Phone.CONTENT_URI,
null, Phone.CONTACT_ID + "=?", new String[] { contactID }, null);
phoneNumCursor.moveToFirst();
Vector<String> phoneTypeList = new Vector<String>();
Vector<String> phoneNumberList = new Vector<String>();
while (!phoneNumCursor.isAfterLast()){
int type = phoneNumCursor.getInt(phoneNumCursor.getColumnIndex(Phone.TYPE));
phoneTypeList.add(String.valueOf(Phone.getTypeLabel(getResources(),type,"")));
phoneNumberList.add(phoneNumCursor.getString(phoneNumCursor.getColumnIndex(Phone.NUMBER)));
phoneNumCursor.moveToNext();
}
//
// Feel free to show label and phone number of that contact. ^^
//
Updated:
Below is an example if you want to use Contact Picker.
private static final int CONTACT_PICKER_RESULT = 1001;
protected void startContactPicker(){
Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,Contacts.CONTENT_URI);
startActivityForResult(contactPickerIntent, CONTACT_PICKER_RESULT);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
switch (requestCode) {
case CONTACT_PICKER_RESULT:
Cursor cursor = null;
String phoneNumber = "";
try {
Uri result = data.getData();
String id = result.getLastPathSegment();
cursor = getContentResolver().query(Phone.CONTENT_URI,
null, Phone.CONTACT_ID + "=?", new String[] { id }, null);
int phoneIdx = cursor.getColumnIndex(Phone.DATA);
if (cursor.moveToFirst()) {
while (!cursor.isAfterLast()){
phoneNumber = cursor.getString(phoneIdx);
//
// this will go through all phoneNumber of selected contact.
//
cursor.moveToNext();
}
}
} catch (Exception e) {
} finally {
if (cursor != null) {
cursor.close();
}
numberView.setText(phoneNumber);
}
break;
}
}
}
这篇关于查询联系人的所有电话号码(解决)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!