What is the best way to get distinct contacts in Android

后端 未结 4 1511
南旧
南旧 2020-12-17 02:34

I am successfully storing contacts in parse.com dashboard data browser by this code.

public void readContacts(){
         ContentResolver cr = getContentReso         


        
4条回答
  •  猫巷女王i
    2020-12-17 02:41

    Here is the Solution that i worked out for you.... You can go through the logcat for information about how it works 100%

    import java.util.ArrayList;
    
    import android.app.Activity;
    import android.database.Cursor;
    import android.net.Uri;
    import android.os.Bundle;
    import android.provider.ContactsContract.CommonDataKinds.Phone;
    import android.provider.ContactsContract.Contacts;
    
    public class MainActivity extends Activity {
        String ClsSimPhonename = null;
        String ClsSimphoneNo = null;
    
        public static ArrayList phonecontact = new ArrayList();
        public static ArrayList simcontact = new ArrayList();
        public static ArrayList totalcontact = new ArrayList();
        public static ArrayList repeatedcontact = new ArrayList();
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            // get phone contact...
            getphonecontact();
            // get sim contact...
            getsimcard_contact();
    
            System.out.println("phone??? " + phonecontact);
            System.out.println("sim??? " + simcontact);
    
            System.out.println("sim_size??? " + simcontact.size());
            System.out.println("phone_size??? " + phonecontact.size());
            System.out.println("totalcontact_size??? " + totalcontact.size());
    
            // filter process beigins here....
            nowFilterContact();
    
        }
    
        private void nowFilterContact() {
            // TODO Auto-generated method stub
    
            // determine which contact have more item....
    
            if (simcontact.size() > phonecontact.size()) {
    
                onemorefiltermethod(simcontact.size(), simcontact, phonecontact);
    
            } else {
                onemorefiltermethod(phonecontact.size(), phonecontact, simcontact);
            }
    
        }
    
        private void onemorefiltermethod(int size, ArrayList contacts,
                ArrayList contact2) {
            // TODO Auto-generated method stub
    
            // compare both contact and get repeated contacts....
            for (int i = 0; i < size; i++) {
    
                try {
                    if (contacts.contains(contact2.get(i))) {
    
                        // add repeated contacts to array....
                        repeatedcontact.add(contact2.get(i));
                    }
                } catch (Exception e) {
    
                }
    
            }
    
            System.out.println("repeatedcontact_size??? " + repeatedcontact.size());
            // now delete repeated contact from total contact
            now_deletedrepeated_contact_from_total();
        }
    
        private void now_deletedrepeated_contact_from_total() {
            // TODO Auto-generated method stub
    
            for (int i = 0; i < totalcontact.size(); i++) {
    
                try {
                    if (totalcontact.contains(repeatedcontact.get(i))) {
                        totalcontact.remove(repeatedcontact.get(i));
    
                    }
                } catch (Exception e) {
    
                }
    
            }
            System.out.println("Final contact size" + totalcontact.size());
    
            System.out.println("Final contact " + totalcontact);
    
        }
    
        private void getsimcard_contact() {
            // TODO Auto-generated method stub
            try {
                Uri simUri = Uri.parse("content://icc/adn");
                Cursor cursorSim = this.getContentResolver().query(simUri, null,
                        null, null, null);
    
                while (cursorSim.moveToNext()) {
                    ClsSimPhonename = cursorSim.getString(cursorSim
                            .getColumnIndex("name"));
                    ClsSimphoneNo = cursorSim.getString(cursorSim
                            .getColumnIndex("number"));
                    ClsSimphoneNo.replaceAll("\\D", "");
                    ClsSimphoneNo.replaceAll("&", "");
                    ClsSimPhonename = ClsSimPhonename.replace("|", "");
    
                    /*
                     * add contact from phone to array phone array and total array
                     */
    
                    phonecontact.add(ClsSimphoneNo);
                    totalcontact.add(ClsSimphoneNo);
    
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
    
        }
    
        private void getphonecontact() {
            // TODO Auto-generated method stub
            try {
                String[] PROJECTION = new String[] { Contacts._ID,
                        Contacts.DISPLAY_NAME, Phone.NUMBER };
    
                Cursor c = managedQuery(Phone.CONTENT_URI, PROJECTION, null, null,
                        null);
                if (c.moveToFirst()) {
                    String ClsPhonename = null;
                    String ClsphoneNo = null;
    
                    do {
                        ClsPhonename = c.getString(c
                                .getColumnIndex(Contacts.DISPLAY_NAME));
                        ClsphoneNo = c.getString(c.getColumnIndex(Phone.NUMBER));
                        /*
                         * add contact from sim to array sim array and total array
                         */
                        simcontact.add(ClsphoneNo);
                        totalcontact.add(ClsphoneNo);
    
                        ClsphoneNo.replaceAll("\\D", "");
                        ClsPhonename = ClsPhonename.replaceAll("&", "");
                        ClsPhonename.replace("|", "");
                        String ClsPhoneName = ClsPhonename.replace("|", "");
    
                    } while (c.moveToNext());
                }
            } catch (Exception e) {
    
            }
        }
    }
    

    Permission

     
    

    Your output is on now_deletedrepeated_contact_from_total() method.

    Check totalcontact array value for Final output

提交回复
热议问题