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