get SMS Contacts - Android Account

Android examples for Account:Contact Get

Description

get SMS Contacts

Demo Code


//package com.java2s;
import java.util.ArrayList;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import android.app.Activity;
import android.content.ContentResolver;

import android.database.Cursor;
import android.provider.ContactsContract;
import android.util.Log;

public class Main {
    private static final String LOG_TAG = "TextAndris_ContactsUtility";

    public static List<String> getSMSContacts(Activity context) {
        List<String> contactNoList = new ArrayList<String>();

        Set<String> smsIds = new HashSet<String>();

        //smsIds = prefs.getStringSet("smsList", new HashSet<String>());
        Cursor cursor;/*from  ww w.j  a va2 s.  c  om*/
        Object[] smsIdList = smsIds.toArray();
        try {
            for (int i = 0; i < smsIdList.length; i++) {
                ContentResolver cr = context.getContentResolver();
                cursor = cr
                        .query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                                new String[] {
                                        ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                                        ContactsContract.CommonDataKinds.Phone.NUMBER },
                                ContactsContract.CommonDataKinds.Phone._ID
                                        + "=?",
                                new String[] { smsIdList[i].toString() },
                                ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
                                        + " ASC");
                if (cursor != null && cursor.getCount() > 0) {
                    cursor.moveToFirst();
                    String name = cursor
                            .getString(cursor
                                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                    String number = cursor
                            .getString(cursor
                                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    String s = name + "\n" + number;
                    contactNoList.add(number);
                }
            }
        } catch (Exception e) {
            Log.d(LOG_TAG,
                    " Error in Contacts Read: " + e.getMessage());
        }

        return contactNoList;
    }
}

Related Tutorials