Android Phone get contacts contact information and SMS broadcast implementation

Source: Internet
Author: User
Tags object object

more and more Android apps are now using their mobile phone numbers to sign up and get their phone verification code to complete their registration. There are many applications to provide phone address Book backup function, get your permission to save your phone number in the address book to the server, you want to be down, for example, QQ, etc. have this function. So how do we get the phone number from the user's address book? Android is ready for us:
---------------------------------get contact information for contacts---------------------------------------------------

First Step plus permissions:

<!--reading Contacts--
<uses-permission android:name= "Android.permission.READ_CONTACTS" ></uses-permission>

Write the code again:

/** contacts on the phone
* External programs can access the data provided by the ContentProvider via the Contentresolver interface,
* Contentresolver instances of the current application can be obtained through getcontentresolver () in activity
* */
cursor cursor = getcontentresolver (). query (ContactsContract.Contacts.CONTENT_URI, NULL, NULL, NULL, and NULL);

StringBuffer buffers = new StringBuffer ();

/** when there is data */

while (Cursor.movetonext ()) {

/** id*/for each contact in the Address Book

String contactId = cursor.getstring (Cursor.getcolumnindex (contactscontract.contacts._id));
/** name of contact in Address Book */
String name = cursor.getstring (Cursor.getcolumnindexorthrow (ContactsContract.Contacts.DISPLAY_NAME));
/** whether the contact has a phone, the result is a string type, 1 indicates yes, 0 is not */
String Hasphone = cursor.getstring (Cursor.getcolumnindex (ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (Hasphone.equalsignorecase ("1"))
Hasphone = "true";
Else
Hasphone = "false";
/** If there is a call, find the contact's phone number according to the contact's ID, the phone can be multiple */
if (Boolean.parseboolean (Hasphone)) {
Cursor mcursor = Getcontentresolver (). query (ContactsContract.CommonDataKinds.Phone.CONTENT_URI, NULL,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + contactId, NULL, NULL);
String PhoneNumber = "";
while (Mcursor.movetonext ()) {
Phonenumber=mcursor.getstring (Mcursor.getcolumnindex (ContactsContract.CommonDataKinds.Phone.NUMBER));

}

/** If a contact has more than one phone, such as a mobile phone and a landline, record it in the form of the {contact: number, contact: number} * *

Buffers.append ("{");
Buffers.append (Api.Contact.CONTACT_NAME). Append (":"). Append (NAME). Append (",");
Buffers.append (Api.Contact.CONTACT_NUM). Append (":"). Append (PhoneNumber);
if (Cursor.islast ()) {
Buffers.append ("}");
} else {
Buffers.append ("},");

}

/** query to the cursor, obtained the corresponding information, can be saved up */

Mcursor.close ();
}
}

---------------------------------use radio to monitor SMS messages---------------------------------------------------

The first step is still to add permissions:

<!--receive SMS-
<uses-permission android:name= "Android.permission.RECEIVE_SMS" ></uses-permission>
<!--texting--
<uses-permission android:name= "Android.permission.SEND_SMS" ></uses-permission>

The second step, write the broadcast code:

public class Smsreceiver extends Broadcastreceiver {


@SuppressLint ("SimpleDateFormat")
@Override
public void OnReceive (Context arg0, Intent arg1) {
/** Get Broadcast Data */
Bundle bundle = Arg1.getextras ();
/** Judgment Data */
if (bundle! = null && bundle.size () > 0) {
/** get an Array object consisting of text messages */
Object[] Objs = (object[]) bundle.get ("PDUs");
for (Object Object:objs) {
Smsmessage mess = SMSMESSAGE.CREATEFROMPDU ((byte[]) object);
/** received SMS content * *
String message = Mess.getmessagebody ();
/** the phone number of the person who sent the message * *
String telephonenum = mess.getdisplayoriginatingaddress ();


/* Reciprocity, let's give him a reply. Message: Now becomes the receiver phone number */
String sendTo = Telephonenum;
/** SMS Manager Smsmanager Object */
Smsmanager Smsmanager = Smsmanager.getdefault ();
/** Add last time */
Date date = new Date (Mess.gettimestampmillis ());
/** Time Format */
SimpleDateFormat format = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
String sendtime = Format.format (date);
/** we want to send each other text message content */
String Text = "Send time:" + sendtime + "\ n Send content:" The message you gave me is: "+message +" I give you back information, please check! ";
/** send a text message out * *
Smsmanager.sendtextmessage (SendTo, NULL, text, NULL, NULL);
This.abortbroadcast ();//Interrupt Broadcast
Break
}
}
}
}

The third step is to register the broadcast in the Androidmanifest.xml:

<!--sign up for SMS broadcast---
<receiver android:name= ". SMS. Smsreceiver "><!--Smsreceiver is written in the SMS file under my corresponding package--
<intent-filter android:priority= ><!--give a priority value, the higher the value, the higher the priority--
<action android:name= "Android.provider.Telephony.SMS_RECEIVED" ></action>
</intent-filter>
</receiver>


Android Phone get contacts contact information and SMS broadcast implementation

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.