Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to retrieve contacts from your android device

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 593
    Comment on it

    Step1: permission needed to read contacts

    <uses-permission android:name="android.permission.READ_CONTACTS"/>

    Step2: ContentResolver provides access to the content provider. Its main work is to read the user's query and redirecting them to the appropriate provider. Here we are using Contentresolver object to get phone contents and Cursor used to get next data in your phone contents.

    ContentResolver contentResolver = this.getContentResolver();
    Cursor phones = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
    
    // we perform loading contacts in background        
    LoadContact loadContact = new LoadContact();
    loadContact.execute();

    Step3: Load data in background, we will perform long running operation in background like getting contact details and adding them to the data class. Now in onPostExecute() method we attach adapter and showing its values into the list.

    class LoadContact extends AsyncTask<Void, Void, Void> {
    
            @Override
            protected Void doInBackground(Void... voids) {
                
    	    // Get Contact list from Phone
                if (phones != null) {
    
                    while (phones.moveToNext()) {
    
                        String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                        String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)).replaceAll(" ", "");
    
    		    // data class to save name and contact number
                        MobileContacts selectUser = new MobileContacts();
                        selectUser.setName(name);
                        selectUser.setPhone(phoneNumber);
                    }
                } else {
                    Toast.makeText(MobileContactsActivity.this, "No contacts in your contact list.", Toast.LENGTH_LONG).show();
                }
                return null;
            }
    
            @Override
            protected void onPostExecute(Void aVoid) {
                super.onPostExecute(aVoid);
    
    	    // now in the adapter class we only have to set name and phone number to the list
                adapter = new ContactsAdapter(selectUsers, MobileContactsActivity.this);
    	    // adding adapter to the listview
                listView.setAdapter(adapter);
            }
        }

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: