Android sends SMS identification number rules to determine the number of characters 70

Source: Internet
Author: User

Public class Duan extends activity {/* declares the variable a button and two edittext */private button mbutton1; private edittext medittext1; private edittext medittext2;/** called when the activity is first created. * // @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main);/** use the findviewbyid constructor to construct * edittext1, edittext2, And button objects */medittext1 = (ED Ittext) findviewbyid (R. id. myedittext1); medittext2 = (edittext) findviewbyid (R. id. myedittext2); mbutton1 = (button) findviewbyid (R. id. mybutton1);/* load the default text into edittext */medittext1.settext ("Enter the number"); medittext2.settext ("Enter the content !! ");/* Set onclicklistener for the user to respond when clicking edittext */medittext1.setonclicklistener (New edittext. onclicklistener () {public void onclick (view v) {/* clear the body when you click edittext */medittext1.settext ("");}}); /* Set onclicklistener to allow the user to respond when clicking edittext */medittext2.setonclicklistener (New edittext. onclicklistener () {public void onclick (view v) {/* clear the body when edittext is clicked */medittext2.settext ("") ;}});/* Set onclicklistener to allow users to click Bu Tton response */mbutton1.setonclicklistener (New button. onclicklistener () {@ override public void onclick (view v) {/* Get the SMS recipient's phone number from edittext1 */string strdestaddress = medittext1.gettext (). tostring ();/* Get text message content from edittext2 */string strmessage = medittext2.gettext (). tostring ();/* construct a smsmanager object for obtaining the default instance */smsmanager = smsmanager. getdefault (); // todo auto-generated method stub/* Check the recipient Whether the telephone format and Text Message count exceed 70 characters */If (isphonenumbervalid (strdestaddress) = true & iswithin70 (strmessage) = true) {try {/** when both conditions are checked and passed, send a text message * first construct a pendingintent object and broadcast it using getbroadcast () * to pendingintent, phone, text message and other parameters * pass in the sendtextmessage () method to send text messages */pendingintent Mpi = pendingintent. getbroadcast (Duan. this, 0, new intent (), 0); smsmanager. sendtextmessage (strdestaddress, null, strmessage, Mpi, null);} catch (exception E) {e. printstacktrace ();} toast. maketext (Duan. This, "sent successfully !! ", Toast. length_short ). show (); medittext1.settext (""); medittext2.settext ("");} when the else {/* phone format and text message do not meet the conditions, toast reminder */If (isphonenumbervalid (strdestaddress) = false) {/* with more than 70 characters */If (iswithin70 (strmessage) = false) {toast. maketext (Duan. this, "the phone number format is incorrect + the text message content exceeds 70 characters. Please check it !! ", Toast. length_short). Show () ;}else {toast. maketext (Duan. This," the phone number format is incorrect. Please check it !! ", Toast. length_short ). show () ;}/ * The number of words exceeds 70 characters */else if (iswithin70 (strmessage) = false) {toast. maketext (Duan. this, "the text message content exceeds 70 characters. Please delete some content !! ", Toast. length_short ). show () ;}}}) ;}/ * check whether the string is a telephone number and return the true or false value */public static Boolean isphonenumbervalid (string phonenumber) {Boolean isvalid = false;/* acceptable phone numbers: * ^ \\(?: You can start with "(" * (\ D {3}): followed by three numbers *\\)?: You can use ")" to connect * [-]?: After the preceding format, you can select "-". * (\ D {3}): followed by three numbers * [-]?: You can choose "-" to connect the device. * (\ D {5}) $: end with five digits. * The following numeric formats can be compared: * (123) 456-7890,123-456-7890,123 4567890, (123)-456-7890 */string expression = "^ \\(? (\ D {3 })\\)? [-]? (\ D {3}) [-]? (\ D {5}) $ ";/* acceptable phone formats: * ^ \\(?: You can start with "(" * (\ D {3}): followed by three numbers *\\)?: You can use ")" to connect * [-]?: After the above format, you can choose "-". * (\ D {4}): followed by four numbers * [-]?: You can choose "-" to connect the device. * (\ D {4}) $: ends with four digits. * The following numeric formats can be compared: * (02) 3456-7890, 02-3456-7890,023 4567890, (02)-3456-7890 */string expression2 = "^ \\(? (\ D {3 })\\)? [-]? (\ D {4}) [-]? (\ D {4}) $ "; charsequence inputstr = phonenumber;/* create pattern */pattern = pattern. compile (expression);/* pass pattern as a parameter to matcher for regular expression */matcher = pattern. matcher (inputstr);/* Create pattern2 */pattern pattern2 = pattern. compile (expression2);/* pass pattern2 as a parameter to matcher2 for regular expression */matcher matcher2 = pattern2.matcher (inputstr); If (matcher. matches () | matcher2.matches () {isvalid = true;} return isvalid;} public static Boolean iswithin70 (string text) {If (text. length () <= 70) {return true;} else {return false ;}}}

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.