프로그램/Android

전화번호부에서 전화번호 긁어오기

잡식성초보 2017. 8. 28. 14:42

프로젝트에서 고객이 원하는 기능은 

웹뷰에서 버튼 클릭시 전화번호부로 이동하여 클릭한 번호를 웹뷰내의 text가 박히는 기능이


었다.이 기능을 구현하기 위해서는 


1. 스킴을 받는것
2. StartActivityResult를 이용하여 전화번호부 띄우기
3. 전화번호를 가져오기.
4. 전화번호를 Webview로 넘기기

이렇게 된다.


1. 웹뷰를 다른 Activity나 Fragment등에서 사용하기 때문에 listener로 데이터를 넘기고 공통으로 쓰는 웹뷰에서 retun값을 준다.



@Override
 public boolean shouldOverrideUrlLoading(WebView view, String url) {
      Log.e(TAG , "shouldOverrideUrlLoading : " + url);
      listener.shouldOverrideUrlLoading(url);

      //전화번호부 열기
      if (url.startsWith("callsend://")) {
          return true;
      }

      return super.shouldOverrideUrlLoading(view, url);
}



2. 웹뷰를 사용하는 곳이다. 웹뷰를 사용하는 Activity가 많다면 Receiver 사용을 추천한다. 


그리고 굳이 공통적인 웹뷰에서 사용하지 않고 뒤로 빼서 사용하는 이유는 StartActivity는 그


냥 사용하면 되지만 전화번호를 갖다오고 난 다음인 StartActivityResult를 사용할수 없으므


로 뒤로 빼서 사용한다. 전화번호부 열기이다.



@Override
public void shouldOverrideUrlLoading(String url) {
      Log.e(TAG, " shouldOverrideUrlLoading : " + url);

      //전화번호부 열기
     if (url.startsWith("callsend://")) {

          Intent intent = new Intent(Intent.ACTION_PICK);
          intent.setData(ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
          startActivityForResult(intent, CALL_LIST_INTENT);
     }
}



3. onAcitivityResult를 이용해서 전화번호를 긁어와야한다.


아래는 onActivityResult를 이용하여 받은 Data를 Uri 형태로 변형하여 ContentResolver 


를 이용하여 전화번호를 획득하며 전화번호 사이에 ㅡ 값을 넣도록 하는 코드도 함께있다. 


리고 완성한 전화번호를 javascript를 이용하여 웹뷰로 전달하고있다.



@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == CALL_LIST_INTENT && resultCode == Activity.RESULT_OK){
       getPhoneNumber(data.getData());
    }
}

private void getPhoneNumber(Uri uri){

        String[] projection = new String[] {
                ContactsContract.CommonDataKinds.Phone.CONTACT_ID, // 연락처 ID -> 사진 정보 가져오는데 사용
                ContactsContract.CommonDataKinds.Phone.NUMBER,        // 연락처
                ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME }; // 연락처 이름.

        ContentResolver contentResolver = mContext.getContentResolver();
        Cursor cursor = contentResolver.query(
                uri, // content://로 시작하는 content table uri
                projection, // 어떤 column을 출력할 것인지
                null,  // 어떤 row를 출력할 것인지
                null,
                null); // 어떻게 정렬할 것인지

        while (cursor.moveToNext()){

            String[] strDDD = {"02" , "031", "032", "033", "041", "042", "043",
                    "051", "052", "053", "054", "055", "061", "062",
                    "063", "064", "010", "011", "012", "013", "015",
                    "016", "017", "018", "019", "070"};

            String strTel = cursor.getString(1).replaceAll("-",
                    "");

            if (strTel.length() < 9) {

            } else if (strTel.substring(0,2).equals(strDDD[0])) {
                strTel = strTel.substring(0,2) + '-' + strTel.substring(2, strTel.length()-4)
                        + '-' + strTel.substring(strTel.length() -4, strTel.length());
            } else {
                for(int i=1; i < strDDD.length; i++) {
                    if (strTel.substring(0,3).equals(strDDD[i])) {
                        strTel = strTel.substring(0,3) + '-' + strTel.substring(3, strTel.length()-4)
                                + '-' + strTel.substring(strTel.length() -4, strTel.length());
                    }
                }
            }

            Log.e(TAG, "tel : " + strTel);

            mWebview.loadUrl("javascript:함수명('" +  strTel + "');");
        }
    }


반응형