SMS receiving, SMS sending

Source: Internet
Author: User

Several ways to send SMS:
The first: Send an implicit intent to access the sent SMS messages from Android

//1Sends an implicit intent to send a short message with the simulator Intent Intent = new Intent (Intent. ACTION_sendto);URI data = URI. Parse("Smsto:"+"mobile phone number");Intent. SetData(data);Intent. PutExtra("Sms_body","SMS Content");StartActivity (Intent);//2Sends an implicit intent to send a short message with the simulator Intent Intent = new Intent (Intent. ACTION_view);Intent. SetType("Vnd.android-dir/mms-sms");Intent. PutExtra("Address","mobile phone number");Intent. PutExtra("Sms_body","SMS Content");StartActivity (Intent);
第二种:用Java代码来实现
Intent Intent = new Intent ("Com.tarena.SENT");pendingintent P1 = pendingintent. Getbroadcast(This,0, Intent,0);Intent Intent2 = new Intent ("Com.tarena.DELIVERY");pendingintent P2 = pendingintent. Getbroadcast(This,0, Intent2,0);Smsmanager Manager = Smsmanager. Getdefault();Manager. Sendtextmessage("15555215556", NULL, etsms. GetText(). toString(), p1, p2);

Note: Several parameters of the Sendtextmessage method are:
1) destinationaddress: Destination Address
2) Scaddress: the equivalent of which mobile base station address, as if it has not been enabled this parameter, so generally null
3) Text: Send SMS Content
Send a text message is generally sent to the carrier's base station, if the base station received, will use the fourth parameter
Send a deferred broadcast to send the status.
4) Sentintent: Send status: Successfully received the message I sent
Send SMS to the carrier's base station, in the base station sent to the destination address, if the destination address received, will use the fifth parameter,
Send a delayed broadcast to send the status I received, but this broadcast will not be sent, generally depending on whether your carrier will send
On the simulator, this parameter is useless;
5) Deliveryintent: Receive Status: Successfully received the message I sent

Note: Using code to send text messages directly, there are two points to note
1) Application Authority send_sms;
2) using the code to send text messages, text messages will not be written to the system data table;
Workaround:
The specific data to be inserted is determined according to your needs:

Contentvalues values = new Contentvalues ();Values. Put("Date", date);Values. Put("Body", body);Values. Put("Address", number);Values. Put("thread_id", thread_id);Values. Put("Person",0);Values. Put("Type",2);Context. Getcontentresolver(). Insert(Uri. Parse("Content://sms/sent"), values);

SMS Receive:
There is a service in the background of the Android system that is dedicated to receiving short messages, and when a new message arrives, the service sends a broadcast broadcast action: "Android.provider.Telephony.SMS_RECEIVED". And will receive a short message as part of the broadcast intent (intent extra) sent out.

A simple example of receiving a text message:
Register a broadcast:

    protectedvoidonResume() {        // TODO Auto-generated method stub        super.onResume();        new MyReciever();        new IntentFilter();        filter.addAction("android.provider.Telephony.SMS_RECEIVED");        filter.setPriority(1001);        registerReceiver(myReciever, filter);    }

Create a broadcast receiver:

 Public  class myreciever extends broadcastreceiver{        @Override         Public void OnReceive(context context, Intent Intent) {//TODO auto-generated method stubString action = Intent.getaction (); ("Android.provider.Telephony.SMS_RECEIVED". Equals (Action) {Bundle bundle = Intent.getextras (); Object [] PDUs = (object[]) Bundle.get ("PDUs"); StringBuilder Builder =NewStringBuilder (); String number ="";//stitching the data inside the PDUs array                 for(inti =0; i < pdus.length; i++) {Smsmessage message = SMSMESSAGE.CREATEFROMPDU ((byte[]) pdus[i]);//SMS contentBuilder.append (Message.getdisplaymessagebody ());//Phone numberNumber = Message.getdisplayoriginatingaddress (); }//intercept SMS, do not allow the system to receive,Abortbroadcast (); }        }    }

Cancel Registration:

@Override    protectedvoidonPause() {        // 解除广播注册        super.onPause();        unregisterReceiver(myReciever);    }

Our program if you want to receive a short message:
1) Register the broadcast receiver and receive Android.provider.Telephony.SMS_RECEIVED broadcast
2) permissions must be set Receive_sms:
3) Set a higher priority (around 1000)
4) If you do not want the system SMS program to receive text messages, the implementation of Abortbroadcast, broadcast stop issued

Question: If the system SMS receiving program does not receive the text message, then this text message will not be written to the database, you can insert into the data table:

Values are determined by themselves:

context.getContentResolver().insert(Uri.parse("content://sms/inbox"), values);

I also found the way, there is a better solution. Also hope to make a lot of suggestions!!

SMS receiving, SMS sending

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.