Android receive and send SMS processing _android

Source: Internet
Author: User
Tags gettext

On the receiving processing of SMS, there are some apps do better, such as the issue of verification code issued to the mobile phone, many apps in the phone received the verification code, do not need to input, you can skip the verification interface, which is used to the processing of the received SMS. As for the message sent, there is nothing to say. Here is only a small example attached.

Effect Chart:

Mainactivity:

Import android.app.Activity; 
Import android.app.PendingIntent; 
Import Android.content.BroadcastReceiver; 
Import Android.content.Context; 
Import android.content.Intent; 
Import Android.content.IntentFilter; 
Import Android.os.Bundle; 
Import Android.telephony.SmsManager; 
Import Android.telephony.SmsMessage; 
Import Android.view.View; 
Import Android.view.View.OnClickListener; 
Import Android.widget.Button; 
Import Android.widget.EditText; 
Import Android.widget.TextView; 
 
Import Android.widget.Toast; 
  public class Mainactivity extends activity {private TextView sender; 
  Private TextView content; 
  Private Intentfilter Receivefilter; 
 
 
  Private Messagereceiver Messagereceiver; 
  Private EditText to; 
  Private EditText msginput; 
  Private Button send; 
  Private Intentfilter Sendfilter; 
 
  Private Sendstatusreceiver Sendstatusreceiver; 
    @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview(R.layout.activity_main); 
    Sender = (TextView) Findviewbyid (R.id.sender); 
    Content = (TextView) Findviewbyid (r.id.content); 
    to = (EditText) Findviewbyid (r.id.to); 
    Msginput = (edittext) Findviewbyid (r.id.msg_input); 
 
    Send = (Button) Findviewbyid (r.id.send); 
    Broadcast receivefilter = new Intentfilter () for receiving SMS settings to be monitored; 
    Receivefilter.addaction ("Android.provider.Telephony.SMS_RECEIVED"); 
    Messagereceiver = new Messagereceiver (); 
 
    Registerreceiver (Messagereceiver, Receivefilter); 
    Set the broadcast sendfilter = new Intentfilter () to be monitored for sending SMS; 
    Sendfilter.addaction ("Sent_sms_action"); 
    Sendstatusreceiver = new Sendstatusreceiver (); 
 
    Registerreceiver (Sendstatusreceiver, Sendfilter); 
        Send.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {//Send SMS 
        And using the fourth parameter of Sendtextmessage to monitor the sending status of SMS Smsmanager Smsmanager = Smsmanager.getdefault (); Intent sentintent = new IntenT ("sent_sms_action"); 
        pendingintent pi = pendingintent.getbroadcast (mainactivity.this, 0, sentintent, 0); 
      Smsmanager.sendtextmessage (To.gettext (). ToString (), NULL, Msginput.gettext (). ToString (), pi, null); 
  } 
    }); 
    } @Override protected void OnDestroy () {Super.ondestroy (); 
    Stop listening for unregisterreceiver (messagereceiver) When the activity is destroyed; 
  Unregisterreceiver (Sendstatusreceiver); Class Messagereceiver extends Broadcastreceiver {@Override public void onreceive (context context, inte 
      NT Intent) {Bundle Bundle = Intent.getextras (); 
 
      Using the PDU secret key to extract a PDUs array object[] PDUs = (object[)) bundle.get ("PDUs"); 
      smsmessage[] messages = new Smsmessage[pdus.length]; 
      for (int i = 0; i < messages.length i++) {Messages[i] = SMSMESSAGE.CREATEFROMPDU ((byte[)) pdus[i]); 
 
    //Get the sender number String address = messages[0].getoriginatingaddress ();  Get SMS content String fullmessage = ""; 
      for (Smsmessage message:messages) {fullmessage + = Message.getmessagebody (); 
      Sender.settext (address); 
    Content.settext (Fullmessage); Class Sendstatusreceiver extends Broadcastreceiver {@Override public void onreceive (context con Text, Intent Intent) {if (getresultcode () = = RESULT_OK) {//Send success Toast.maketext (context, "Send s 
      Ucceeded ", Toast.length_long). Show (); 
      else {//Send failed Toast.maketext (context, "Send failed", Toast.length_long). Show ();  } 
    } 
 
  } 
 
}

Activity_main:

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Match_parent" android:layout_height= "match_parent" android:orientation= "vertical" > <linearlayout Android:layout_widt 
      H= "Match_parent" android:layout_height= "50DP" > <textview android:layout_width= "wrap_content" 
      android:layout_height= "Wrap_content" android:layout_gravity= "center_vertical" android:padding= "10DP" Android:text= "from:"/> <textview android:id= "@+id/sender" android:layout_width= "Wrap_conte" NT "android:layout_height=" Wrap_content "android:layout_gravity=" center_vertical "/> </LinearLayout > <linearlayout android:layout_width= "match_parent" android:layout_height= "50DP" > <text View android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:layout_gravity= "Center_vertical" android:padding= "10DP" android:text= "Content:"/> <textview android:id= "@+id/content" Andr Oid:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:layout_gravity= "center_vertical" "/> </LinearLayout> <linearlayout android:layout_width=" Match_parent "android:layout_height 
      = "50DP" > <textview android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:layout_gravity= "center_vertical" android:padding= "10DP" android:text= "to:"/> <edi Ttext android:id= "@+id/to" android:layout_width= "0DP" android:layout_height= "Wrap_content" and Roid:layout_gravity= "Center_vertical" android:layout_weight= "1"/> </LinearLayout> <linearlayou T android:layout_width= "match_parent" android:layout_height= "50DP" > <edittext android:id= "@ +id/msg_input "anDroid:layout_width= "0DP" android:layout_height= "wrap_content" android:layout_gravity= "center_vertical" android:layout_weight= "1"/> <button android:id= "@+id/send" android:layout_width= "Wrap_conten" T "android:layout_height=" wrap_content "android:layout_gravity=" center_vertical "android:text=" Send " 
 /> </LinearLayout> </LinearLayout>

Androidmanifest:

<?xml version= "1.0" encoding= "Utf-8"?> <manifest xmlns:android= "http://schemas.android.com/apk/res/" 
    Android "package=" Com.example.smstest "android:versioncode=" 1 "android:versionname=" 1.0 "> <uses-sdk android:minsdkversion= "android:targetsdkversion="/>//Receive SMS <uses-permission android:name= "a 
 
  Ndroid.permission.RECEIVE_SMS "/>//Send SMS <uses-permission android:name=" Android.permission.SEND_SMS "/> <application android:allowbackup= "true" android:icon= "@drawable/ic_launcher" android:label= "@string/ap  
      P_name "Android:theme=" @style/apptheme "> <activity android:name=" com.example.smstest.MainActivity " Android:label= "@string/app_name" > <intent-filter> <action android:name= "Android.inten T.action.main "/> <category android:name=" Android.intent.category.LAUNCHER "/> </intent-filt Er> </activity>;/application> </manifest>  

The above is the entire content of this article, I hope to help you learn.

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.