Thursday 23 June 2016

How to Get Number From Contact Uri or Id Android


https://dedyyuristiawan.blogspot.co.id


Hello world. Today i wanna show you, how to get number from contact uri for develop android app. I write this post because yesterday i got problem when i want to get number from contact uri. Now i have solution to solve this problem. Yoyoy lets do this.

So yesterday i found library for show a name from the contact, but this library it just show name not a number, and i want to get number if i click name contact from list. In my context is i just have contact uri, so how can i get number from the contact ? after i find from the google , i wrap it to function. This is my function getNumber.

private String getNumber(int id){
    String strid = Long.toString(id); // convert id to string
    String phoneNumber=""; // create variable
    Cursor cursor = getActivity().getContentResolver().query(
    ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
    null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
    new String[]{strid}, null); // Query for get record from id
        if(cursor.moveToFirst()){
            int phoneColumn = cursor.getColumnIndex("data1");
            // get number from first index
            phoneNumber = cursor.getString(phoneColumn);
            // get string from from number
            Log.d("DATA",phoneNumber);
            // test log, check its right number ?
        }
    return phoneNumber; // return number
}

And i call my function like this.

getNumber(Integer.valueOf(contact.contactUri.getLastPathSegment())
// getLastPathSegment its for get id from contact uri
// convert to int

Yoyoy this my fuction to get number from id number. The conclusion is  if you have just contact uri you can use this 'getLastPathSegment()' function for get id, and then from the id you can get number from my function. If you have confuse about my post you can write comment bellow, i hope i can help you more. Thanks.
Share:
Lokasi: Semarang, Semarang City, Central Java, Indonesia

1 comment:


<br>


Advice for contacting me at dedyy54@gmail.com