In this tutorial, I will walk you through using the Android Contact
Picker to let your users choose a phone number from their contact book.
Upon finishing this tutorial, you can follow our Android calling tutorials
to make a call with Sinch.

If you get stuck at any point, you can check out the finished source
code on our
GitHub.

Move back over to your main activity java file. In onCreate, set an
onClickListener for the button. When the button is clicked, you want to
create an intent that opens the contact book and returns a result when a
user is clicked.

//declare globally, this can be any intpublicfinalintPICK_CONTACT=2015;//onCreate(findViewById(R.id.button)).setOnClickListener(newView.OnClickListener(){@OverridepublicvoidonClick(Viewv){Intenti=newIntent(Intent.ACTION_PICK,ContactsContract.CommonDataKinds.Phone.CONTENT_URI);startActivityForResult(i,PICK_CONTACT);}});

After the user has chosen a contact, you need to parse the result
returned from startActivityForResult for a phone number.

To ready this phone number to be used to make a call with Sinch, you
will use an AsyncTask to make a call to our phone number normalization
API. See the comments below for an explanation of what’s happening:

classnormalizePhoneNumberTaskextendsAsyncTask<String,Void,String>{//input your app key and secret from the Sinch dashboardprivateStringappKey="your_app_key";privateStringappSecret="your_app_secret";//takes phone number string as an argument@OverrideprotectedStringdoInBackground(String...params){StringnormalizedPhoneNumber="";try{//get ready to make a get request to normalize the phone numberHttpClienthttpclient=newDefaultHttpClient();HttpGethttpGet=newHttpGet("https://callingapi.sinch.com/v1/calling/query/number/"+params[0].replaceAll("\\s+",""));//sinch uses basic authenticationStringusernamePassword="application:"+appKey+":"+appSecret;Stringencoded=Base64.encodeToString(usernamePassword.getBytes(),Base64.NO_WRAP);httpGet.addHeader("Authorization","Basic "+encoded);//handle the responseHttpResponseresponse=httpclient.execute(httpGet);ResponseHandler<String>handler=newBasicResponseHandler();//parse JSON response from Sinch to get phone numbernormalizedPhoneNumber=parseJSONResponse(handler.handleResponse(response));}catch(ClientProtocolExceptione){Log.d("ClientProtocolException",e.getMessage());}catch(IOExceptione){Log.d("IOException",e.getMessage());}returnnormalizedPhoneNumber;}//once the asynctask is complete, display a toast message with the normalized phone number@OverrideprotectedvoidonPostExecute(StringnormalizedPhoneNumber){//if you want to make a call with sinch, this is the place to do it!Toast.makeText(getApplicationContext(),normalizedPhoneNumber,Toast.LENGTH_LONG).show();}//the sinch api returns a json like {"number":{"restricted":false,"countryId":"US","numberType":"Mobile","normalizedNumber":"+16507141052"}}//this method will return a string of just the phone number, +16507141052privateStringparseJSONResponse(StringjsonString){StringreturnString="";try{JSONObjectjsonObject=newJSONObject(jsonString);returnString=jsonObject.getJSONObject("number").getString("normalizedNumber");}catch(JSONExceptione){Log.d("JSONException",e.getMessage());}returnreturnString;}}

Now, you can run the app, choose a contact, and a toast message of the
normalized number will pop up.

I hope this gives a good example of how to use the Android Contact
Picker. Next up, we will be using Sinch to make a phone call with that
number. Head over to our Android tutorial.