Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • LibPhoneNumber library implementation - An easy way to fetch phone contacts

    • 0
    • 0
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 572
    Comment on it

    Are you Looking for a better library that can fetch all phone contacts easily and that can manage all numbers in common formats. Then LibPhoneNumber library is a better solution because this library is faster then other handmade queries. This library is approved by Google and also used bt WhatsApp and in many apps.

    Add compile lib in app gradle file : compile 'com.googlecode.libphonenumber:libphonenumber:7.0.4'

    Create instance of PhoneNumberUtil class like this

        PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
    

    Create instance of static and serialized class named PhoneNumber

         Phonenumber.PhoneNumber phNumberProto = null;
    

    Now get you country so that you can manage all contacts according to your country format like this

         String countryCode = Locale.getDefault().getCountry();
    

    and then here you can parse all phone number as

      try {
                     phNumberProto = phoneUtil.parse(phoneNumber, countryCode);
                        } catch (NumberParseException e) {
                            e.printStackTrace();
                            AppLogs.printLogs("err", "number parsing err  " + phoneNumber);
           }
    

    Now you convert phone contacts in International format , National format , E164 format etc like this

         if (phNumberProto != null){
                        boolean isValid = phoneUtil.isValidNumber(phNumberProto);
    
                        if (isValid) {
                            String internationalFormat = phoneUtil.format(phNumberProto, PhoneNumberUtil.PhoneNumberFormat.E164);
                            data.setmContactNumber(internationalFormat.trim());
                            AppLogs.printLogs("phone", "phone  --> " + internationalFormat);
                            mListContacts.add(data);
    
                        }else{
                            AppLogs.printLogs("invalid phone", "invalid phone  --> ");
                        }
    
                    }
    

    For your help i am sharing complete code hope it will help you

      ArrayList<ContactsData> mListContacts = new ArrayList<ContactsData>();
    
        public ArrayList<ContactsData> allPhoneContact(Activity ctx) {
    
            ContentResolver cr = ctx.getContentResolver();
            Cursor cur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT&#95;URI, null, null, null, null);
            PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
    
            if (cur.getCount() > 0) {
    
                while (cur.moveToNext()) {
                    ContactsData data = new ContactsData();
                    String name = cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY&#95;NAME));
                    String phoneNumber = cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
    
                    if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.HAS&#95;PHONE&#95;NUMBER))) > 0) {
    
                        if (name.contains("\\s+")){
                            String [] nameArr = name .split("\\s+");
                            data.setmContactFName(nameArr[0]);
                            data.setmContactLName(nameArr[1]);
                            AppLogs.printLogs("name", "Fname --> " + nameArr[0]);
                            AppLogs.printLogs("name", "Lname --> " + nameArr[1]);
                        }else{
                            data.setmContactFName(name);
                            data.setmContactLName(" ");
                            AppLogs.printLogs("name", "name --> " + name);
    
                        }
    
                        Phonenumber.PhoneNumber phNumberProto = null;
                        String countryCode = Locale.getDefault().getCountry();
    
                        try {
                            phNumberProto = phoneUtil.parse(phoneNumber, countryCode);
                        } catch (NumberParseException e) {
                            e.printStackTrace();
                            AppLogs.printLogs("err", "number parsing err --> " + phoneNumber);
                        }
    
                        if (phNumberProto != null){
                            boolean isValid = phoneUtil.isValidNumber(phNumberProto);
    
                            if (isValid) {
                                String internationalFormat = phoneUtil.format(phNumberProto, PhoneNumberUtil.PhoneNumberFormat.E164);
                                data.setmContactNumber(internationalFormat.trim());
                                AppLogs.printLogs("phone", "phone  --> " + internationalFormat);
                                mListContacts.add(data);
    
                            }else{
                                AppLogs.printLogs("invalid phone", "invalid phone  --> ");
                            }
    
                        }
    
                    }
                }
                cur.close();
            }
            return mListContacts;
        }
    

 1 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: