The implementation method is simple
1. Save the contacts and phone numbers in the Address Book to the TXT file to complete the backup.
2. Read the TXT file and import to the Address Book to complete the restore.
Specific code
1. Add read and write access to the address book, memory card Write permissions
<uses-permission android:name= "Android.permission.READ_CONTACTS" ></uses-permission>
<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE" ></uses-permission>
<uses-permission android:name= "Android.permission.WRITE_CONTACTS" ></uses-permission>
2. Write the file code
File Savefile=new file ("/sdcard/test.txt");
FileOutputStream OutStream;
try {
OutStream = new FileOutputStream (saveFile);
Outstream.write (Str.getbytes ());
Outstream.close ();
} catch (Exception e) {
Settitle (E.tostring ());
}
3. Access Contacts
Str= "";
Cursor cur = getcontentresolver (). query (ContactsContract.Contacts.CONTENT_URI, NULL, NULL, NULL, or NULL);
if (Cur.movetofirst ()) {
int idcolumn = Cur.getcolumnindex (contactscontract.contacts._id);
int displaynamecolumn = Cur.getcolumnindex (ContactsContract.Contacts.DISPLAY_NAME);
do {
String contactId = cur.getstring (Idcolumn);
String disPlayName = cur.getstring (Displaynamecolumn);
Str+=displayname;
int phonecount = Cur.getint (Cur.getcolumnindex (ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (phonecount>0) {
Cursor phones = getcontentresolver (). Query (Contactscontract.commondatakinds.phone.content_uri,null, Contactscontract.commondatakinds.phone.contact_id+ "=" + contactId, NULL, NULL);
int i=0;
String PhoneNumber;
if (Phones.movetofirst ()) {
do{
i++;
Phonenumber= phones.getstring (Phones.getcolumnindex (ContactsContract.CommonDataKinds.Phone.NUMBER));
if (i==1)
Str=str+ "," +phonenumber;
System.out.println (PhoneNumber);
}while (Phones.movetonext ());
}
}
str+= "\ r \ n";
} while (Cur.movetonext ());
}
}
4. Read the file code
try {
File File = new file ("/sdcard/test.txt");
FileInputStream instream = new FileInputStream (file);
Bytearrayoutputstream OutStream = new Bytearrayoutputstream ();
byte[] buffer = new BYTE[1024*5];
int length =-1;
while (length = instream.read (buffer))! =-1) {
Outstream.write (buffer, 0, length);
}
Outstream.close ();
Instream.close ();
String txt= outstream.tostring ();
} catch (IOException e) {
Settitle (E.tostring ());
}
5. Write your Contacts
Contentvalues values = new Contentvalues ();
Uri Rawcontacturi = Getcontentresolver (). Insert (Rawcontacts.content_uri, values);
Long Rawcontactid = Contenturis.parseid (Rawcontacturi);
Values.clear ();
Values.put (data.raw_contact_id, Rawcontactid);
Values.put (Data.mimetype, Structuredname.content_item_type);
Values.put (Structuredname.given_name, NAME);
Getcontentresolver (). Insert (Data.content_uri, values);
Values.clear ();
Values.put (data.raw_contact_id, Rawcontactid);
Values.put (Data.mimetype, Phone.content_item_type);
Values.put (Phone.number, num);
Values.put (Phone.type, phone.type_home);
Getcontentresolver (). Insert (Data.content_uri, values);
Android phone Address Book backup restore code
Free in
Both the user name and password are
specific download directory in /pub/android source collection/2011/November/android phone Address Book backup restore code/
Android phone Address Book backup restore code