Read the Local Address Book of the mobile phone by using

Source: Internet
Author: User

Public class pimtest extends MIDlet implements commandlistener {

Private display;

Private form;

Private command exit;

Command OK;

Public pimtest (){
Display = display. getdisplay (this );
OK = new command ("read", command. OK, 1 );
Exit = new command ("exit", command. Exit, 1 );
Form = new form ("read Address Book ");
Form. addcommand (OK );
Form. addcommand (exit );
Form. setcommandlistener (this );

}

Protected void destroyapp (Boolean arg0 ){
}

Protected void pauseapp (){
}

Protected void Startapp () throws midletstatechangeexception {
Display. setcurrent (form );
}

/**
* Read the address book
*
*/
Private void readaddresslist (){

String [] names = Pim. getinstance (). listpimlists (PIM. contact_list); // get the contact list name

For (INT I = 0; I <names. length; I ++ ){
Try {
Form. append ("name =" + Names [I] + "/N ");
Contactlist list = (contactlist) Pim. getinstance (). openpimlist (
Pim. contact_list, Pim. read_only, Names [I]);
Contact contact;
Enumeration E = List. Items ();
Int n = 0; // number of records
While (E. hasmoreelements ()){
Contact = (contact) E. nextelement ();
String name = NULL;
Try {

// Obtain the name
String [] arraystr = contact. getstringarray (
Contact. Name, 0 );

If (arraystr! = NULL & arraystr. length! = 0 ){
Name = "";
// The first five subscripts are valid on n78 and correspond to: 0 surname, 1 Name, 2 intermediate name, 3 title, and 4 title.
For (Int J = 0; j <arraystr. length; j ++ ){
If (arraystr [J]! = NULL
& Arraystr [J]. Length ()! = 0 ){
Name + = J + "." + arraystr [J];
}
}
}

} Catch (exception exc ){
Form. append ("error:" + exc. tostring () + "/N ");
}

Try {
If (name = NULL) {// if the name is not obtained, change it to formatted_name.
If (list. issupportedfield (contact. formatted_name )){
Name = contact. getstring (
Contact. formatted_name, 0 );
}
}

} Catch (exception exc ){
Form. append ("error:" + exc. tostring () + "/N ");
}

Form. append ("name =" + name + "/N ");

If (list. issupportedfield (contact. Tel) {// obtain Tel
String Tel = contact. getstring (contact. Tel, 0 );
Form. append ("Tel =" + Tel + "/N ");
}

N ++; // The number of records increases
}

Form. append ("Total" + N + "records/N ");
List. Close ();
} Catch (pimexception e ){
Form. append ("pimexception:" + E. tostring ());
E. printstacktrace ();
} Catch (exception e ){
Form. append ("exception:" + E. tostring ());
E. printstacktrace ();
}
}

}

Public void commandaction (command C, displayable d ){
If (C = exit ){
Destroyapp (true );
Yydestroyed ();
} Else if (C = OK ){
Readaddresslist ();
}
}

}

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/kf156/archive/2009/09/14/4551131.aspx

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.