我想从我的联系人列表中选择一个联系人。 我读了很多解决方案和研究几周,但所有文章都没有正常工作。 一些代码如下:
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
//并且在activityresult中:
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);
if (c.moveToFirst()) {
String name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
tv1.setText(name);
}
}
或此代码获取所有联系人但我不能拥有联系人数量:
String[] contacts = new String[] {People.NAME, People.NUMBER};
Uri contentUri = People.CONTENT_URI;
Cursor cursor = managedQuery(contentUri, contacts, null, null, null);
String textContacts = "";
if (cursor.moveToFirst()) {
String myname = null;
String mynumber = null;
do {
textContacts = textContacts + cursor.getString(cursor.getColumnIndex(People.NAME)) + " : " + cursor.getString(cursor.getColumnIndex(People.NUMBER)) + "\n";
} while (cursor.moveToNext());
tv1.setText(textContacts);
}
谁能帮助我PLZ?我的机器人是2.3.3
答案 0 :(得分:50)
尝试以下代码可以帮助您:
// You need below permission to read contacts
<uses-permission android:name="android.permission.READ_CONTACTS"/>
// Declare
static final int PICK_CONTACT=1;
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
//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();
cNumber = phones.getString(phones.getColumnIndex("data1"));
System.out.println("number is:"+cNumber);
}
String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
}
}
break;
}
}
答案 1 :(得分:18)
使用Intent.ACTION_PICK访问手机联系人。代码为
Uri uri = Uri.parse("content://contacts");
Intent intent = new Intent(Intent.ACTION_PICK, uri);
intent.setType(Phone.CONTENT_TYPE);
startActivityForResult(intent, REQUEST_CODE);
哪里
private static final int REQUEST_CODE = 1;
使用onActivityResult()方法访问所选联系人。代码为
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent intent) {
if (requestCode == REQUEST_CODE) {
if (resultCode == RESULT_OK) {
Uri uri = intent.getData();
String[] projection = { Phone.NUMBER, Phone.DISPLAY_NAME };
Cursor cursor = getContentResolver().query(uri, projection,
null, null, null);
cursor.moveToFirst();
int numberColumnIndex = cursor.getColumnIndex(Phone.NUMBER);
String number = cursor.getString(numberColumnIndex);
int nameColumnIndex = cursor.getColumnIndex(Phone.DISPLAY_NAME);
String name = cursor.getString(nameColumnIndex);
Log.d(TAG, "ZZZ number : " + number +" , name : "+name);
}
}
};
答案 2 :(得分:11)
添加将联系人数据读取到应用程序清单的权限。
<uses-permission android:name="android.permission.READ_CONTACTS"/>
在您的活动中使用
Intent.Action_Pick
,如下所示
Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,
ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
startActivityForResult(contactPickerIntent, RESULT_PICK_CONTACT);
然后覆盖
onActivityResult()
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// check whether the result is ok
if (resultCode == RESULT_OK) {
// Check for the request code, we might be usign multiple startActivityForReslut
switch (requestCode) {
case RESULT_PICK_CONTACT:
Cursor cursor = null;
try {
String phoneNo = null ;
String name = null;
Uri uri = data.getData();
cursor = getContentResolver().query(uri, null, null, null, null);
cursor.moveToFirst();
int phoneIndex =cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
phoneNo = cursor.getString(phoneIndex);
textView2.setText(phoneNo);
} catch (Exception e) {
e.printStackTrace();
}
break;
}
} else {
Log.e("MainActivity", "Failed to pick contact");
}
}
这将检查出来
答案 3 :(得分:3)
以下是我实施的内容:
private String[] getContactList(){
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
Log.i(LOG_TAG, "get Contact List: Fetching complete contact list");
ArrayList<String> contact_names = new ArrayList<String>();
if (cur.getCount() > 0)
{
while (cur.moveToNext())
{
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER.trim())).equalsIgnoreCase("1"))
{
if (name!=null){
//contact_names[i]=name;
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",new String[]{id}, null);
while (pCur.moveToNext())
{
String PhoneNumber = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
PhoneNumber = PhoneNumber.replaceAll("-", "");
if (PhoneNumber.trim().length() >= 10) {
PhoneNumber = PhoneNumber.substring(PhoneNumber.length() - 10);
}
contact_names.add(name + ":" + PhoneNumber);
//i++;
break;
}
pCur.close();
pCur.deactivate();
// i++;
}
}
}
cur.close();
cur.deactivate();
}
String[] contactList = new String[contact_names.size()];
for(int j = 0; j < contact_names.size(); j++){
contactList[j] = contact_names.get(j);
}
return contactList;
}
您可以调用此功能,也可以使用AutoCompleteTextView显示和选择联系人姓名和10位数字。这个函数返回String数组,你可以直接返回arrayList并删除最后一个for循环。
答案 4 :(得分:2)
如果您想在点击或焦点事件中从联系人列表中选择联系人,请复制此代码并粘贴到您的活动中。
编辑文本的焦点事件:
phoneNo.setOnFocusChangeListener(new OnFocusChangeListener()
{ public void onFocusChange(View v, boolean hasFocus)
{
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent,PICK_CONTACT );//PICK_CONTACT is private static final int, so declare in activity class
} });
获取联系人姓名和电话的功能:
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 phn_no = phones.getString(phones.getColumnIndex("data1"));
String name = c.getString(c.getColumnIndex(StructuredPostal.DISPLAY_NAME));
Toast.makeText(this, "contact info : "+ phn_no+"\n"+name, Toast.LENGTH_LONG).show();
}
}
}
}
}
答案 5 :(得分:1)
这项工作对我来说
获取手机号码和姓名
startActivityForResult(new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI), TYPE_CONTACT);
案例TYPE_CONTACT: uriContact = data.getData();
String name=retrieveContactName();
System.out.println("name = " + name);
String number=retrieveContactNumber();
System.out.println("number = " + number);
break;
} }
private String retrieveContactNumber(){
String contactNumber = null;
// getting contacts ID
Cursor cursorID = getContentResolver().query(uriContact,
new String[]{ContactsContract.Contacts._ID},
null, null, null);
if (cursorID.moveToFirst()) {
contactID = cursorID.getString(cursorID.getColumnIndex(ContactsContract.Contacts._ID));
}
cursorID.close();
Log.d(TAG, "Contact ID: " + contactID);
// Using the contact ID now we will get contact phone number
Cursor cursorPhone = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER},
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ? AND " +
ContactsContract.CommonDataKinds.Phone.TYPE + " = " +
ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE,
new String[]{contactID},
null);
if (cursorPhone.moveToFirst()) {
contactNumber = cursorPhone.getString(cursorPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
cursorPhone.close();
Log.d(TAG, "Contact Phone Number: " + contactNumber);
return contactNumber;
}
private String retrieveContactName() {
String contactName = null;
// querying contact data store
Cursor cursor = getContentResolver().query(uriContact, null, null, null, null);
if (cursor.moveToFirst()) {
// DISPLAY_NAME = The display name for the contact.
// HAS_PHONE_NUMBER = An indicator of whether this contact has at least one phone number.
contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
}
cursor.close();
Log.d(TAG, "Contact Name: " + contactName);
return contactName;
}
答案 6 :(得分:1)
获取手机号码和电子邮件地址
//获取电话号码
name = getIntent().getExtras().getString("name");
id = getIntent().getExtras().getString("contactid");
Cursor cursorPhone = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER},
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ? AND " +
ContactsContract.CommonDataKinds.Phone.TYPE + " = " +
ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE,
new String[]{id.toString()},
null);
String contactNumber = null;
if (cursorPhone.moveToFirst()) {
contactNumber = cursorPhone.getString(cursorPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
System.out.println("contactnum"+contactNumber);
}
}
cursorPhone.close();
//获取电子邮件地址
Cursor emailCursor = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", new String[]{id}, null);
if (emailCursor.moveToFirst()) {
String phone = emailCursor.getString(emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
int type = emailCursor.getInt(emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
String s = (String) ContactsContract.CommonDataKinds.Email.getTypeLabel(AddContactActivity.this.getResources(), type, "");
etEmail.setText(phone);
}
emailCursor.close();
}
答案 7 :(得分:1)
此代码适用于手机号码联系人,不适用于电子邮件或其他内容。我发现这个代码最简单。如果有任何问题,请告诉我。
使用电话数据类型的选择意图启动活动:
findViewById(R.id.choose_contact_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent pickContact = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
startActivityForResult(pickContact, PICK_CONTACT_REQUEST);
}
});
现在设置onAcitivityResult();
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent){
switch (requestCode){
case PICK_CONTACT_REQUEST:
if (resultCode == RESULT_OK){
Uri contactUri = intent.getData();
Cursor nameCursor = getContentResolver().query(contactUri, null, null, null, null);
if (nameCursor.moveToFirst()){
String name = nameCursor.getString(nameCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String number = nameCursor.getString(nameCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
((EditText)findViewById(R.id.person_name)).setText(name);
((EditText)findViewById(R.id.enter_mobile)).setText(number);
nameCursor.close();
}
}
break;
}
}
答案 8 :(得分:0)
尝试以下代码,
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(ContactsContract.Contacts._ID);
startActivityForResult(intent, PICK_CONTACT);
public void onActivityResult(int requestCode, int resultCode, Intent intent)
{
if (requestCode == PICK_CONTACT)
{
Cursor cursor = managedQuery(Email.CONTENT_URI, null, Email.CONTACT_ID + " = " + intent.getData(), null, null);
cursor.moveToNext();
String contactId = cursor.getString(cursor.getColumnIndex(Email._ID));
String name = cursor.getString(cursor.getColumnIndexOrThrow(Email.DATA1));
Toast.makeText(this, "Contect LIST = "+name, Toast.LENGTH_LONG).show();
}
}
答案 9 :(得分: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());
}
}
答案 10 :(得分:0)
如果没有清单或运行时权限,您可以尝试此操作。
从联系人应用中提取姓名和联系电话。通过此代码,联系人应用程序还将显示联系人姓名下方的数字。
代码是从联系人应用中选择单个姓名和号码。
private void openContactSelectionIntent() {
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(Intent.createChooser(intent, "Select Contact"), RC_SELECT_CONTACT);
}
这是onActivityResult
方法的代码
Uri uri1 = data.getData();
Cursor c = managedQuery(uri1, null, null, null, null);
if (c.moveToFirst()) {
String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
System.out.println("name is: " + name);
}
Cursor cursor = getContentResolver().query(uri1, null, null, null, null);
if (null == cursor) return;
try {
while (cursor.moveToNext()) {
String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
System.out.println("number is "+number);
}
}catch (Exception e){
e.printStackTrace();
}finally {
cursor.close();
}
答案 11 :(得分:0)
如果联系人确实有多个号码,您也可以尝试此解决方案
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
startActivityForResult(intent , REQUEST_CODE_ADDRESS_BOOK
);
,然后显示活动结果:
@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode) {
case (REQUEST_CODE_ADDRESS_BOOK):
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor c = getContentResolver().query(contactData, null, null, null, null);
c.moveToFirst();
String phoneNumber = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
c.close();
}
break;
}
}
然后,该号码将在phoneNumber变量中。
答案 12 :(得分:0)
if (requestCode == RESULT_CONTACTS && resultCode == Activity.RESULT_OK) {
Uri uri;
Cursor cursor1, cursor2;
String TempNameHolder, TempNumberHolder, TempContactID, IDresult = "";
int IDresultHolder;
uri = data.getData();
cursor1 = getContentResolver().query(uri, null, null, null, null);
if (cursor1.moveToFirst()) {
TempNameHolder = cursor1.getString(cursor1.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
mToMeetName = TempNameHolder.trim();
TempContactID = cursor1.getString(cursor1.getColumnIndex(ContactsContract.Contacts._ID));
IDresult = cursor1.getString(cursor1.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
IDresultHolder = Integer.valueOf(IDresult);
if (IDresultHolder == 1) {
cursor2 = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + TempContactID, null, null);
while (cursor2.moveToNext()) {
TempNumberHolder = cursor2.getString(cursor2.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
mToMeetName = TempNumberHolder.trim();
mEditTextToMeet.setText(TempNumberHolder + "( " + TempNameHolder + " )");
//number.setText(TempNumberHolder);
}
}
}
}
答案 13 :(得分:0)
您可以使用 LiveData 从“联系人”列表中获取姓名和电话号码,还可以检查“联系人”权限。
ReadContactsViewModel
public class ReadContactsViewModel extends AndroidViewModel {
private MutableLiveData<ArrayList<Contat>> contactList;
public ReadContactsViewModel(@NonNull Application application) {
super(application);
}
public MutableLiveData<ArrayList<Contat>> getContacts(Context context) {
if (contactList == null) {
String phoneNumber;
ArrayList<Contat> savedContacts = new ArrayList<>();
Contat savedContact;
ContentResolver cr = context.getContentResolver();
ArrayList<String> idList = new ArrayList<>();
if (cr != null) {
Cursor cur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, "UPPER(" + ContactsContract.Contacts.DISPLAY_NAME + ") ASC");
if (cur != null) {
while (cur.moveToNext()) {
phoneNumber = cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
if (phoneNumber.length() > 9) {
savedContact = new Contat();
phoneNumber = phoneNumber.replaceAll("\\s+", "");
String id = phoneNumber.substring(phoneNumber.length() - 7);//last 7 digits of numbers
String name = cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String photoThumb = cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_THUMBNAIL_URI));
savedContact.setNumber(phoneNumber);
savedContact.setName(name);
if (!idList.contains(id)) {
savedContacts.add(savedContact);
idList.add(id);
}
}
}
cur.close();
idList.clear();
}
}
contactList = new MutableLiveData<>();
contactList.setValue(savedContacts);
}
return contactList;
}
现在您可以获取此savedContactList
或将其发送到服务器,我还使用了权限TedPermission
库,您可以使用任何喜欢的人
private void syncContacts() {
ReadContactsViewModel readContactsViewModel = ViewModelProviders.of(this)
.get(ReadContactsViewModel.class);
Observer<ArrayList<Contat>> readContactsObserver =
new Observer<ArrayList<Contat>>() {
@Override
public void onChanged(@Nullable ArrayList<Contat> savedContactList) {
assert savedContactList != null;
for (int i = 0; i < savedContactList.size(); i++) {
Contat savedContact = savedContactList.get(i);
contactPostList.add(new Contat(savedContact.getName(), savedContact.getNumber()));
}
youractiviy.this.postContactsToServer();
}
};
contactPermissionListener = new PermissionListener() {
@Override
public void onPermissionGranted() {
readContactsViewModel.getContacts(youractiviy.this)
.observe(youractiviy.this,
readContactsObserver);
}
@Override
public void onPermissionDenied(ArrayList<String> deniedPermissions) {
}
};
TedPermission.with(this).setPermissionListener(contactPermissionListener).setDeniedMessage(R.string.permission_denied).setPermissions(Manifest.permission.READ_CONTACTS).check();
}
答案 14 :(得分:0)
add_filter( 'admin_body_class', 'add_body_classes' );
function add_body_classes( $classes ) {
if (get_current_screen()->post_type == 'post') {
global $post;
foreach((get_the_category($post->ID)) as $category) {
if($category->name == 'download')
{
$classes .= 'download';
}
}
}
return $classes;
}
答案 15 :(得分:0)
这是我为所有在Kotlin寻找解决方案的人的答案, 不要与“绑定”一词混淆,这只是用于绑定视图。点击“ pickContact”,我们将打开“联系人”应用。
首先在清单文件中添加权限。
<uses-permission android:name="android.permission.READ_CONTACTS"/>
val RESULT_PICK_CONTACT = 1
binding?.pickContact ->{
val contactPickerIntent = Intent(
Intent.ACTION_PICK,
ContactsContract.CommonDataKinds.Phone.CONTENT_URI
)
startActivityForResult(contactPickerIntent, RESULT_PICK_CONTACT)
}
点击pickContact,我们将打开“联系人”应用, 然后覆盖onActivityResult方法,
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == Activity.RESULT_OK) {
when (requestCode) {
RESULT_PICK_CONTACT -> {
var cursor: Cursor? = null
try {
var phoneNo: String? = null
var name: String? = null
val uri: Uri? = data?.data
cursor = contentResolver.query(uri!!, null, null, null, null)
cursor?.moveToFirst()
val phoneIndex: Int =
cursor!!.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)
val phoneContactName: String =
cursor!!.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME))
phoneNo = cursor?.getString(phoneIndex)
et_name.setText(phoneContactName) //setting the contacts name
mobile_number.setText(phoneNo) //setting the contacts phone number
} catch (e: Exception) {
e.printStackTrace()
}
}
}
} else {
Log.e("ActivityName", "Something went wrong")
}
}
如果您发现任何问题,请在下面发表评论,我会回答您的查询。