Android 연락처 이름과 전화 코드 가 져 오기

6360 단어 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
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기