lundi 22 juin 2015

Best way to sync contacts

I'm reading each and every contact details and then sending the HTTP request. If the contact number matches then I will return the data and those data will be stored in the SQLite Database. However I have 2200 + contacts in my phone and it's taking 20 to 25 minutes to sync from Localhost. It's fine if I search for individual contacts. But for every contact it takes too much time!

Here is my code:

Function : getContacts() : This function will be called in Async_task class

       private void getContacts()
        {
            uFunctions obj = new uFunctions();

            ContentResolver cr = getContentResolver();
            Cursor cur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
            if (cur.getCount() > 0)
            {
                while (cur.moveToNext())
                {
                    String number = cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                    String mainNumber = number.replaceAll("[^0-9]+", "");

                    if(dbHelper.CheckIsDataAlreadyInDBorNot())
                    {
                       //Already exists

                    }
                    else
                    {
                        String getData;
                        try
                        {
//Sending Request and getting jSON response 
                            getData = obj.getPhoneNumberNick(mainNumber);
                            JSONObject _jObject  = new JSONObject(getData);
                            JSONObject _jObjectSub  = _jObject.getJSONObject("0");
                            String _emailUsers = _jObjectSub.getString("email");
                            String _cell_noUsers = _jObjectSub.getString("cell_no");

dbHelper.insertContact(_cell_noUsers, _emailUsers);

                        }
                        catch(JSONException e)
                        {
                            //Failed
                            e.printStackTrace();
                        }

                    }



                    Log.i("Phone number", mainNumber);


                }
            }

            cur.close();
        }

Asyc_task class :

 public class async_task extends AsyncTask<Void,Void,Void>
    {
        private ProgressDialog nDialog;

        protected void onPreExecute(){
            super.onPreExecute();
            nDialog = ProgressDialog.show(ContactsSync.this,"Sync","Loading");

        }

        @Override
        protected Void doInBackground(Void... params) {
            getContacts();
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {

            super.onPostExecute(aVoid);
            nDialog.dismiss();

        }
    }

And then at last calling this method at on the onCreate() method

 protected void onCreate(Bundle savedInstanceState)
    {
        dbHelper = new dbopen(getApplicationContext());
        dbHelper.open();

        super.onCreate(savedInstanceState);
        setContentView(R.layout.contact_sync);

        new async_task().execute();

    }

It's taking too much time. Please help :(

Aucun commentaire:

Enregistrer un commentaire