Thread: Contacts backup

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    2
    Rep Power
    0

    Wink Contacts backup


    Hi Developers, I am new Android Developer..Learning to build Android Applications.. i am stuck with the Problem, i want to write code which will do the following task... upon opening the application and clicking the start button the code should upload all the contacts and sms in the mobile to FTP Server, IMAP or Any other Online Backup like Dropbox e.t.c with the progress bar... i dono how to start with this... please somebody help me...
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    15
    Rep Power
    0
    You just need to do some searching around. To retrieve SMS and contacts from the android phone,
    Taken from (http://stackoverflow.com/questions/7204035/how-to-access-sms-and-contacts-data),

    Code:
    Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
    ArrayList<HashMap<String,String>> contactData=new ArrayList<HashMap<String,String>>();
             while (cursor.moveToNext()) {
                 try{
                 String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); 
                 String name=cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                 String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)); 
                 if (Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                     Cursor phones = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, null, null);
                     while (phones.moveToNext()) { 
                         String phoneNumber = phones.getString(phones.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER));
                         HashMap<String,String> map=new HashMap<String,String>();
                         map.put("name", name);
                         map.put("number", phoneNumber);
                         contactData.add(map);
                     } 
                     phones.close(); 
                 }
             }catch(Exception e){}
             }
    Code:
    Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
    ArrayList<HashMap<String,String>> contactData=new ArrayList<HashMap<String,String>>();
             while (cursor.moveToNext()) {
                 try{
                 String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); 
                 String name=cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                 String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)); 
                 if (Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                     Cursor phones = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, null, null);
                     while (phones.moveToNext()) { 
                         String phoneNumber = phones.getString(phones.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER));
                         HashMap<String,String> map=new HashMap<String,String>();
                         map.put("name", name);
                         map.put("number", phoneNumber);
                         contactData.add(map);
                     } 
                     phones.close(); 
                 }
             }catch(Exception e){}
             }
    As for uploading them, you could compile all this into one file and upload it to a server.
    You could just use a simple POST request.

IMN logo majestic logo threadwatch logo seochat tools logo