Android 연락처 이름과 전화 코드 가 져 오기
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="40dp"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:paddingLeft="10dp"
android:text=" :"
android:textColor="@android:color/black"
android:textSize="13sp" />
<EditText
android:id="@+id/et_name"
android:layout_width="200dp"
android:layout_height="fill_parent"
android:layout_marginLeft="10dp" />
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text=" " />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="40dp"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:paddingLeft="10dp"
android:text=" :"
android:textColor="@android:color/black"
android:textSize="13sp" />
<EditText
android:id="@+id/et_phone"
android:layout_width="200dp"
android:layout_height="fill_parent"
android:layout_marginLeft="10dp"
android:inputType="phone" />
</LinearLayout>
</LinearLayout>
이것 이 바로 일반적인 레이아웃 파일 코드 입 니 다.
/**
*
*
* @param cursor
* @param context
* @return
*/
private ContactBen getContactPhone(Cursor cursor, Context context) {
ContactBen vo = new ContactBen();
int phoneColumn = cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER);
int phoneNum = 0;
try {
phoneNum = cursor.getInt(phoneColumn);
} catch (Exception e) {
return null;
}
// String phoneResult = "";
if (phoneNum > 0) {
// ID
int idColumn = cursor.getColumnIndex(ContactsContract.Contacts._ID);
String contactId = cursor.getString(idColumn);
vo.name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
// cursor;
Cursor phones = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);
if (phones.moveToFirst()) {
//
for (; !phones.isAfterLast(); phones.moveToNext()) {
int index = phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
int typeindex = phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE);
int phone_type = phones.getInt(typeindex);
String phoneNumber = phones.getString(index);
switch (phone_type) {
case 2:
vo.phone = phoneNumber;
break;
}
}
if (!phones.isClosed()) {
phones.close();
}
}
}
return vo;
}
여 기 는 주요 기능 의 코드 입 니 다.여기 서 try catch 동작 을 해 야 합 니 다.안 드 로 이 드 폰 은 위 챗 과 qq 의 연락처 도 목록 에 추가 하기 때 문 입 니 다.하지만 사실은 전화번호 가 없어 서 되 돌아 오 려 면 클릭 하지 못 하고 try catch 가 없 으 면 이상 을 알 립 니 다.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case (1): {
if (resultCode == Activity.RESULT_OK) {
if (data != null) {
Uri contactData = data.getData();
@SuppressWarnings("deprecation")
Cursor c = MainActivity.this.managedQuery(contactData, null, null, null, null);
c.moveToFirst();
ContactBen contactPhone = getContactPhone(c, MainActivity.this);
if (contactPhone == null) {
contactPhone = new ContactBen();
}
et_name.setText("" + contactPhone.name);
et_phone.setText("" + contactPhone.phone);
}
}
break;
}
}
}
이것 은 값 을 가 져 오 는 반전 입 니 다.이 반전 에서 원 하 는 데 이 터 를 얻 을 수 있 습 니 다.
findViewById(R.id.btn1).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
requestPermission(new String[] { Manifest.permission.READ_CONTACTS }, new PermissionHandler() {
@Override
public void onGranted() {
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, 1);
}
@Override
public void onDenied() {
super.onDenied();
}
});
}
});
여 기 는 클릭 이벤트 의 처리 입 니 다.안 드 로 이 드 6.0 과 6.0 이상 시스템 권한 이 적당 합 니 다.마지막 으로 목록 파일 에 해당 하 는 권한 을 추가 하 는 것 을 기억 하 십시오:최종 효 과 는 다음 과 같다.
원본 주소:contactperson
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Bitrise에서 배포 어플리케이션 설정 테스트하기이 글은 Bitrise 광고 달력의 23일째 글입니다. 자체 또는 당사 등에서 Bitrise 구축 서비스를 사용합니다. 그나저나 며칠 전 Bitrise User Group Meetup #3에서 아래 슬라이드를 발표했...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.