Android implementation to send short message function example detailed _android

Source: Internet
Author: User
Tags gettext stub

In this paper, we analyze the method of Android to send short message function. Share to everyone for your reference, specific as follows:

SMS and phone calls are the basic features of Android phones, and here are examples of how Android can deliver text messages.

The program looks like this:

Import Java.util.regex.Matcher;
Import Java.util.regex.Pattern;
Import android.app.Activity;
Import android.app.PendingIntent;
Import android.content.Intent;
Import Android.graphics.Color;
Import Android.os.Bundle;
Import Android.telephony.SmsManager;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.EditText;
Import Android.widget.Toast;
 public class A03activity extends activity {private EditText et01,et02;
  Private Button B; /** called the activity is a.
    * * @Override public void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
    Setcontentview (R.layout.main);
    et01= (EditText) Findviewbyid (r.id.et01);
    Et02= (EditText) Findviewbyid (r.id.et02);
    b= (Button) Findviewbyid (R.ID.B);
    B.setbackgroundcolor (Color.green);
    B.settext ("send");
    Et01.settext ("Please enter the telephone number");//original edittext inside the hint content et02.settext ("Please enter the text message content"); The EditText method below is used when the contents of the edit box are clicked and the original content disappears into the editState Et01.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {//TODO auto-generated
  Method stub Et01.settext ("");
    }
    });  Et02.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {//TODO auto-generated method
  Stub Et02.settext ("");
    }
    }); The Setonclicklistener () method of the Button is to trigger the sending of SMS event B.setonclicklistener (new Onclicklistener () {@Override public void OnC
  Lick (View v) {//TODO auto-generated method Stub String S01=et01.gettext (). toString ();
  String S02=et02.gettext (). toString ();
  Obtains a default instance of Smsmanager Smsmanager Sm=smsmanager.getdefault (); if (Isphonenumbervalid (S01) &&iswithin70 (S02)) {/** * When all two criteria are passed, a Pendingintent object is constructed and the Getbroadcast () is used. Broadcast * Then the pendingintent, SMS, phone number, etc. are passed into the Smsmanager sendtextmessage () method/try {pendingintent Pi=pendingintent.getbroad
   Cast (a03activity.this, 0, New Intent (), 0);
   Sm.sendtextmessage (S01, NULL, S02, PI, null); Catch (Exception e)
   {//TODO auto-generated catch block E.printstacktrace ();
  Toast.maketext (a03activity.this, "SMS sent successfully", Toast.length_long). Show (); } else{if (Isphonenumbervalid (S01) ==false) {if isWithin70 (S02) ==false) {Toast.maketext (a03activity.this, "phone number Bad format! SMS content more than 70 words! Please correct!!!
   ", Toast.length_long). Show (); else{Toast.maketext (a03activity.this, "phone number format is wrong!) Please correct!!!
   ", Toast.length_long). Show (); } else{if (IsWithin70 (S02) ==false) {Toast.maketext (a03activity.this, "SMS content more than 70 words!)
   Please correct ", Toast.length_long). Show ();
  }
   }
  }
  }
    });
   //Determine if the message content is more than 70 words public static Boolean isWithin70 (String s) {if (s.length () >70) {return false;
   } else{return true;
   }///Determine if the phone number is in the correct format public static Boolean isphonenumbervalid (String phonenumber) {Boolean valid=false; /** * Two phone number formats * ^\\ (?) indicates that the following 3 digits (* \ \) can be followed by (opening * (\\d{3})? means to continue * [-]? Indicates that the selective "-" continuation * (\\d{4}) can be used after the above format to indicate that the following 4 digits are followed by * [-]? TableIn the following format, you can use the selective "-" continuation * (\\d{4}) $ to end with 4 digits * To sum up: the correct phone number format can be used as a reference: * (123) 456-78900 123-456-78900 1234567 8900 (123) -456-78900*/String expression01= "^\\" ( \\D{3}) \)? [- ]? (\\d{4}) [- ]?
   (\\d{4}) $ "; String expression02= "^\\ ( \\D{3}) \)? [- ]? (\\d{5}) [- ]?
   (\\d{5}) $ ";
   Create pattern Object pattern P01=pattern.compile (EXPRESSION01);
   Pass pattern as a parameter to Matcher, as the correct format for the phone number PhoneNumber Matcher m01=p01.matcher (PhoneNumber);
   Pattern P02=pattern.compile (EXPRESSION02);
   Matcher M02=p02.matcher (PhoneNumber); if (m01.matches () | |
   M02.matches ()) {valid=true;
   } else{Valid=false;
  return valid;

 }
}

Androidmanifest.xml is as follows:

<?xml version= "1.0" encoding= "Utf-8"?> <manifest xmlns:android=
"http://schemas.android.com/apk/res/" Android "
  package=" com.my.a03 "
  android:versioncode=" 1 "
  android:versionname=" 1.0 ">
  < USES-SDK android:minsdkversion= "Ten"/>
  <application
    android:icon= "@drawable/ic_launcher"
    Android:label= "@string/app_name" >
    <activity
      android:name= ". A03activity "
      android:label=" @string/app_name ">
      <intent-filter>
        <action android:name= "Android.intent.action.MAIN"/>
        <category android:name= "Android.intent.category.LAUNCHER"/>
      </intent-filter>
    </activity>
  </application>
  <uses-permission android:name= " Android.permission.SEND_SMS "></uses-permission>
</manifest>

When a Pendingintent object is received, the broadcast action is performed, as is the Context.sendbroadcast () method, which is why the Smsmanager.sendtextmessage () method to pass in Pendingintent as one of the parameters of the service.

I wrote the program directly into the test via my Android smartphone, and it doesn't matter if there is no Android 2.3.3 version of the smartphone, and you can open two Android simulators, one for sending messages and one for receiving information in the following ways:

First, run the program with Eclipse compilation, and then open the first emulator

Second, open the DOS window (CMD), and enter the following command:
D:\>CD D:\SDK\android\tools\

Three, Input shell command
D:\sdk\android\tools>emulator-data Foo

After these steps, a second emulator will appear. Through the upper left Instanceid (such as: 5546) as the recipient's phone number, you can test the status of message delivery.

Sometimes we write text messages are likely to be more than 70 words, and we do not want a paragraph of input, want to directly input, and then let the phone automatically help us split into 70 characters a text message, this How to do?

Here's a way to use Smsmanager:

Public arraylist<string> dividemessage (String text)

When the message content more than 70 words, this method will automatically help us to the text into a few more than 70 text messages, and the Dividemessage () method of the return type is ArrayList, and then through the Sendtextmessage () to do the loop to send.

The last thing to do is to add the ability to send text messages in Androidmanifest, as follows:

<uses-permission
android:name= "Android.permission.SEND_SMS" ></uses-permission>

For more information on Android-related content readers can view the site topics: "Android Resource Operation tips Summary", "Android Development introduction and Advanced Course", "Android Control Usage Summary", "Android SMS and telephone operating skills summary" and " Android Multimedia How-to Summary (audio, video, recording, etc.)

I hope this article will help you with the Android program.

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.