Android several ways to send text messages "Turn"

Source: Internet
Author: User

Sending text messages in Android is simple,

The first thing to do is to add the required permissions in the Mainfest.xml:

<uses-permissionAndroid:name= "Android.permission.SEND_SMS"></uses-permission><uses-permissionAndroid:name= "Android.permission.READ_SMS"></uses-permission><uses-permissionAndroid:name= "Android.permission.RECEIVE_SMS"></uses-permission>

For later testing convenience, simply add all the SMS permissions.

Method 1:

 Public class extends Activity {...      Private void send1 (string phone, string message) {         = pendingintent.getactivity (Thisnew Intent ( this, testsms. class), 0);          = Smsmanager.getdefault ();          NULL NULL );     } }

Method 2:

If you want to know the status after the text message is sent, you need two receiver to realize

1 Private voidSend2 (string number, String message) {2String SENT = "Sms_sent";3String delivered = "sms_delivered";4     5Pendingintent SENTPI = pendingintent.getactivity ( This, 0,NewIntent (SENT), 0);6Pendingintent deliveredpi = pendingintent.getactivity ( This, 0,NewIntent (Delivered), 0);7     8Registerreceiver (NewBroadcastreceiver () {9  Ten @Override One              Public voidOnReceive (Context context, Intent Intent) { A                 Switch(Getresultcode ()) -                 { -                      CaseACTIVITY.RESULT_OK: theLOG.I ("====>", "ACTIVITY.RESULT_OK"); -                          Break; -                      Casesmsmanager.result_error_generic_failure: -LOG.I ("====>", "Result_error_generic_failure"); +                          Break; -                      CaseSmsmanager.result_error_no_service: +LOG.I ("====>", "Result_error_no_service"); A                          Break; at                      CaseSMSMANAGER.RESULT_ERROR_NULL_PDU: -LOG.I ("====>", "RESULT_ERROR_NULL_PDU"); -                          Break; -                      CaseSmsmanager.result_error_radio_off: -LOG.I ("====>", "Result_error_radio_off"); -                          Break; in                 } -             } to},NewIntentfilter (SENT)); +      -Registerreceiver (NewBroadcastreceiver () { the @Override *          Public voidOnReceive (Context context, Intent Intent) { $             Switch(Getresultcode ())Panax Notoginseng             { -                  CaseACTIVITY.RESULT_OK: theLOG.I ("====>", "RESULT_OK"); +                      Break; A                  Caseactivity.result_canceled: theLOG.I ("=====>", "result_canceled"); +                      Break; -             } $         } $},NewIntentfilter (Delivered)); -      -Smsmanager SMSM =Smsmanager.getdefault (); theSmsm.sendtextmessage (number,NULL, message, SENTPI, deliveredpi); -}

In the simulator is not see receiver in the log information, according to the internet said on the mobile phone can be achieved, only a pity the hands of inorganic, can only be armchair.

Method 3:

Above are sent text files, if you want to send some non-text, such as encrypted data, you can use the following methods:

Private void Send2 (string number, String message) {    = smsmanager.getdefault ();      Short port = +;     = Pendingintent.getbroadcast (test.  thisnew Intent (), 0);     NULL NULL );}

Method 4:

Call the system's SMS interface, this method requires the user to enter the receiver's phone number

1 Private void Send (String message) {2     New Intent (intent.action_view); 3     Sendintent.putextra ("Sms_body", message); 4     Sendintent.settype ("vnd.android-dir/mms-sms"); 5 }

This method automatically sets the number of the receiving party

1 Private void Send1 (string number, String message) {2     Uri uri = Uri.parse ("Smsto:" + number ); 3     New Intent (Intent.action_view, URI); 4     Sendintent.putextra ("Sms_body", message); 5     startactivity (sendintent); 6 }

Text message acceptance, need to implement Broadcastreceiver class, monitoring system messages

First, add a declaration in Mainfest.xml, Smsreceiver for the implementation of the class

<android:name= ". Smsreceiver "><intent-filter>    <  Android:name = "Android.provider.Telephony.SMS_RECEIVED" /> </ Intent-filter > </ receiver >

If this is method 3, you need to join when using Senddatamessage

<receiverAndroid:name=". Smsreceiver "> <Intent-filter>    <ActionAndroid:name= "Android.intent.action.DATA_SMS_RECEIVED" />    <DataAndroid:scheme= "SMS" />    <DataAndroid:host= "localhost" />    <DataAndroid:port= "+" /></Intent-filter> </receiver>

The DAT data is commented out, the simulator can also receive text messages; port is not a problem when it is inconsistent with the ports value in Senddatamessage, it's amazing.

 Public classSmsreceiverextendsBroadcastreceiver {@Override Public voidOnReceive (Context context, Intent Intent) {Bundle bundle=Intent.getextras (); Smsmessage[] Msgs=NULL;        String phone;                        String message; if(Bundle! =NULL) {object[] PDUs= (object[]) bundle.get ("PDUs")); Msgs=NewSmsmessage[pdus.length];  for(inti = 0; i < msgs.length; i++) {Msgs[i]= SMSMESSAGE.CREATEFROMPDU ((byte[]) pdus[i]); Phone=msgs[i].getoriginatingaddress (); Message=Msgs[i].getmessagebody (); }        }    }}

If it is Senddatamessage sent:

1  Public classSmsreceiverextendsBroadcastreceiver {2  3 @Override4      Public voidOnReceive (Context context, Intent Intent) {5Bundle bundle =Intent.getextras ();6smsmessage[] Msgs =NULL;7 String phone;8 String message;9                 Ten         if(Bundle! =NULL){ OneObject[] PDUs = (object[]) bundle.get ("PDUs"); AMsgs =NewSmsmessage[pdus.length]; -              for(inti = 0; i < msgs.length; i++){ -Msgs[i] = SMSMESSAGE.CREATEFROMPDU ((byte[]) pdus[i]); thePhone =msgs[i].getoriginatingaddress (); -                 byteData[] = SMSMESSAGE.CREATEFROMPDU ((byte[]) pdus[i]). Getuserdata (); -Message =NewString (data); -             } +         } -     } +}

A different place is when a message is acquired using the Getuserdata () method.

Translated from: http://blog.csdn.net/rangq1/article/details/5793953

Android several ways to send text messages "Turn"

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.