android 电话id,从具有ID,Android的联系人提供商中检索电话...

我可以检索联系人ID,但是稍后我希望根据联系人ID分别检索电话号码.下面的代码返回电话号码的空结果. (我确实希望稍后再检索名称和电话号码并填充视图,但是我只是想让电话号码首先起作用).

在我的onCreate中,我有此代码

String phoneNum = getPhoneNumber(myID);

TextView phoneTextView = (TextView) findViewById(R.id.textViewPhone);

phoneTextView.setText(phoneNum);

这是getPhoneNumber()的方法

protected String getPhoneNumber(String id) {

ArrayList phones = new ArrayList();

Cursor cursor = getContentResolver().query(

ContactsContract.CommonDataKinds.Phone.CONTENT_URI,

null,

ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",

new String[]{id}, null);

while (cursor.moveToNext()) {

phones.add(cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));

}

cursor.close();

String phoneNum;

phoneNum = phones.get(0);

return phoneNum;

}//end getPhoneNumber();

}

这将产生错误java.lang.IndexOutOfBoundsException:无效的索引0,大小为0,我打算为其创建一些错误处理.但是,我仍然确定我具有前面代码中的ID,所以我不知道为什么ArrayList返回null.如果您想查看该代码,它也位于我的onCreate中:

Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);

if (cursor.getCount() != 0) {

int numContacts = cursor.getCount();

ArrayList idList = new ArrayList<>();

Random rand = new Random();

int randomNum = rand.nextInt(numContacts);

while (cursor.moveToNext()) {

String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));

idList.add(id);

}

myID = idList.get(randomNum);

String myString = Integer.toString(randomNum);

TextView myTextView = (TextView) findViewById(R.id.textViewID);

myTextView.setText(myString);

if (myID != null) {

myTextView.setText(myID);

} else {

myTextView.setText("Try Again!");

}

} else {

Toast.makeText(getApplicationContext(), "Your have no contacts.", Toast.LENGTH_SHORT).show();

}

cursor.close();

最佳答案

// You can fetch the Contact Number and Email With Following Methods.

String phone = getPhoneNumber(ContactId);

String email = getEmail("" + ContactId);

private String getPhoneNumber(long id) {

String phone = null;

Cursor phonesCursor = null;

phonesCursor = queryPhoneNumbers(id);

if (phonesCursor == null || phonesCursor.getCount() == 0) {

// No valid number

//signalError();

return null;

} else if (phonesCursor.getCount() == 1) {

// only one number, call it.

phone = phonesCursor.getString(phonesCursor

.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

} else {

phonesCursor.moveToPosition(-1);

while (phonesCursor.moveToNext()) {

// Found super primary, call it.

phone = phonesCursor.getString(phonesCursor

.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

break;

}

}

return phone;

}

private Cursor queryPhoneNumbers(long contactId) {

ContentResolver cr = getContentResolver();

Uri baseUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI,

contactId);

Uri dataUri = Uri.withAppendedPath(baseUri,

ContactsContract.Contacts.Data.CONTENT_DIRECTORY);

Cursor c = cr.query(dataUri, new String[]{ContactsContract.CommonDataKinds.Phone._ID, ContactsContract.CommonDataKinds.Phone.NUMBER,

ContactsContract.CommonDataKinds.Phone.IS_SUPER_PRIMARY, ContactsContract.RawContacts.ACCOUNT_TYPE,

ContactsContract.CommonDataKinds.Phone.TYPE,

ContactsContract.CommonDataKinds.Phone.LABEL},

ContactsContract.Data.MIMETYPE + "=?",

new String[]{ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE}, null);

if (c != null && c.moveToFirst()) {

return c;

}

return null;

}

private String getEmail(String id) {

String email = "";

ContentResolver cr = getContentResolver();

Cursor emailCur = cr.query(

ContactsContract.CommonDataKinds.Email.CONTENT_URI,

null,

ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",

new String[]{id}, null);

while (emailCur.moveToNext()) {

// This would allow you get several email addresses

// if the email addresses were stored in an array

email = emailCur.getString(

emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));

// String emailType = emailCur.getString(

// emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));

}

emailCur.close();

return email;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值