안드로이드 주소록 불러오는 예제

Posted by Breeze24
2016. 6. 4. 23:19 Android/TroubleShooting
안드로이드 주소록 불러오는 예제

안드로이드 주소록을 호출하여 선택된 전화번호와 이름을 가져온다. 


Button searchButton = (Button) findViewById(R.id.addressBtn);

searchButton.setOnClickListener(new OnClickListener() {

             

            public void onClick(View v) {

                // TODO Auto-generated method stub

                

                Intent intent = new Intent(Intent.ACTION_PICK, Phone.CONTENT_URI);

                startActivityForResult(intent, ComConstants.ADDRESS_BOOK_CODE);

            }

        });

}

 

    @Override

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if(requestCode == ComConstants.ADDRESS_BOOK_CODE){

            if(resultCode == RESULT_OK){

                if(data != null){

                    Uri addressData = data.getData();

                    Cursor cusor = managedQuery(addressData, null, null, null, null);

                    if(cusor.moveToFirst()){

                        String phoneNum = cusor.getString(cusor.getColumnIndex(Phone.NUMBER));

                        String name = cusor.getString(cusor.getColumnIndex(Phone.DISPLAY_NAME));

                        

                    }

                }

            }

        }

    }
.