Android mobile phone Address Book backup and restoration code

Source: Internet
Author: User

Recently, I want to write an android program for fun.

Development Environment eclipse, android2.2

Development Environment Construction

1. Install JDK first

2. Download and install eclipse

3. download and install the android SDK

4. Install the Eclipse plug-in ADT

5. Configure the android SDK path in WINDOW> preferences

6. Create AVD

 

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 it to the address book for restoration.

 

Code

1. Add the address book read/write permission and the memory card write permission.

<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 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. Retrieve the Address Book Contact

STR = "";
Cursor cur = getcontentresolver (). Query (contactscontract. Contacts. content_uri, 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. example. 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 [1, 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 the address book

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 );

If you find anything unreasonable, need to improve the place, or you have any better implementation method mail contact 328452421@qq.com (qq perennial not online, mail contact) Zhu Xiao
. Exchange with each other. Thank you.

 

Source http://download.csdn.net/detail/xiaoxiao108/3741045

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.