Android Get phone Address Book, SIM card contact and call Dialing interface method _android

Source: Internet
Author: User

Android Get phone Address book contact information

Copy Code code as follows:

private void Getphonecontacts () {
Contentresolver resolver = This.getcontentresolver ();

Get Mobile Contact
Cursor phonecursor = Resolver.query (Phone.content_uri,
New string[] {phone.contact_id, phone.display_name,
Phone.number},
Phone.display_name + "=?" + "and" + phone.type + "= '"
+ Phone.type_mobile + "'", new string[] {name}, NULL);
if (phonecursor!= null) {
while (Phonecursor.movetonext ()) {
String number = phonecursor.getstring (2);
When the cell phone number is empty or skips the current loop for an empty field
if (Textutils.isempty (PhoneNumber))
Continue
Get Contact Name
String username = phonecursor.getstring (1);
Mcontactsname.add (ContactName);
Mcontactsnumber.add (PhoneNumber);

}
Phonecursor.close ();
}
}

Get phone SIM card contact information

SIM card and mobile phone I get similar way just URL a bit different, need to note that the SIM card is not contact person avatar.

Copy Code code as follows:

private void Getsimcontacts () {
Contentresolver resolver = Mcontext.getcontentresolver ();
Get Sims Card Contacts
Uri uri = uri.parse ("Content://icc/adn");
Cursor phonecursor = Resolver.query (URI,
New string[] {phone.contact_id, phone.display_name,
Phone.number},
Phone.display_name + "=?" + "and" + phone.type + "= '"
+ Phone.type_mobile + "'", new string[] {name}, NULL);
if (phonecursor!= null) {
while (Phonecursor.movetonext ()) {
String number = phonecursor.getstring (2);
When the cell phone number is empty or skips the current loop for an empty field
if (Textutils.isempty (PhoneNumber))
Continue
Get Contact Name
String username = phonecursor.getstring (1);
Mcontactsname.add (ContactName);
Mcontactsnumber.add (PhoneNumber);

}
Phonecursor.close ();
}
}

call the system to call the interface, the code is as follows.
Tel: Telephone number
Copy Code code as follows:

Call system method to call
Intent dialintent = new Intent (Intent.action_call, Uri.parse ("Tel:" + mcontactsnumber.get (position)));
StartActivity (dialintent);

Finally, do not forget to add permissions in the Androidmanifest.xml file, otherwise run the program is an error!

Copy Code code as follows:

<!--read Contact permissions-->
<uses-permission android:name= "Android.permission.READ_CONTACTS"/>
<!--call permission-->
<uses-permission android:name= "Android.permission.CALL_PHONE"/>

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.