0

I am new to the android.Here I am create the apps such as read the phone contacts and stored into the csv file format file .It takes a long time to read contacts..In mean time i am need to use the progress bar until the file is writing the contacts details.Now i am confused that where can i insert the progress bar in my code..

My coding is...

  private String getPrimaryNumber(long _id) {
           String primaryNumber = null;
           try {
               Cursor cursor = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                       new String[]{Phone.NUMBER, Phone.TYPE},
                       ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ _id, // We need to add more selection for phone type
                       null,
                       null);
               if(cursor != null) {
                   while(cursor.moveToNext()){
                       switch(cursor.getInt(cursor.getColumnIndex(Phone.TYPE))){
                           case Phone.TYPE_MOBILE :
                               primaryNumber = cursor.getString(cursor.getColumnIndex(Phone.NUMBER));
                               break;
                           case Phone.TYPE_HOME :
                               primaryNumber = cursor.getString(cursor.getColumnIndex(Phone.NUMBER));
                               break;
                           case Phone.TYPE_WORK :
                               primaryNumber = cursor.getString(cursor.getColumnIndex(Phone.NUMBER));
                               break;
                           case Phone.TYPE_OTHER :
                       }
                       if(primaryNumber != null)
                           break;
                   }
               }       
           } catch (Exception e) {
               Log.i("test", "Exception " + e.toString());
           } finally {
               if(cursor != null) {
                   cursor.deactivate();
                   cursor.close();             
               }
           }
           return primaryNumber;
       }

It is the code to read the contacts from the phone.In that coding where i can add the progress bar... please help anyone.. thanks in advance.

Sagar Maiyad
  • 12,655
  • 9
  • 63
  • 99
rajesh
  • 29
  • 2
  • 10

2 Answers2

0

You need to implement an AsyncTask and show your progress bar.

Here's a nice tutorial.

user2336315
  • 15,697
  • 10
  • 46
  • 64
0

Try this:

 protected Void doInBackground(Void... params) {
        ContentResolver cr = getContentResolver();
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
                null, null, null);


    if (cur.getCount() > 0) {
        while (cur.moveToNext()) {
            String id = cur.getString(cur
                    .getColumnIndex(ContactsContract.Contacts._ID));
            String name = cur
                    .getString(cur
                            .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            if (Integer
                    .parseInt(cur.getString(cur
                            .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                System.out.println("name : " + name + ", ID : " + id);
                // Result=Result+ "Name: "+name;
                // get the phone number
                Cursor pCur = cr.query(
                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                        null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                + " = ?", new String[] { id }, null);
                while (pCur.moveToNext()) {
                    String phone = pCur
                            .getString(pCur
                                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                    publishProgress("Name:" + name + ",Number:" + phone
                            + "\n");
                }
                pCur.close();
            }
        }
    }
    return null;
}
Sagar Maiyad
  • 12,655
  • 9
  • 63
  • 99