Package Com.example.demo;
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.net.Uri;
Import Android.os.Bundle;
Import Android.telephony.SmsManager;
Import Android.telephony.SmsMessage;
Import Android.util.Log;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.EditText;
public class Mainactivity extends Activity {
Private Button btnsend;private EditText etcontent;private myreciever receiver; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Btnsend= (Button) Findviewbyid (R.id.button1); Etcontent= (EditText) Findviewbyid (R.ID.EDITTEXT1); Btnsend.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {SENDSM S (); } });} todoprotected void Sendsms () {//System SMS Send confidence */* * * */Intent intent=new Intent (intent.action_sendto); Uri data=uri.parse ("Smsto:" +5554); Intent.setdata (data); Intent.putextra ("Sms_body", Etcontent.gettext ()); StartActivity (Intent); /* * Send Message two */
//Intent intent01=new Intent (intent.action_sendto);
//Intent01.settype ("vnd.android-dir/mms-sms");
//Intent01.putextra ("Address", "155555251552");
//Intent01.putextra ("Sms_body", Etcontent.gettext (). toString ());
//StartActivity (INTENT01);
///2) Use the code to send text messages, request permission, use the code to send text messages will not be written to the System data table
/*
* SMS receive. There is a service in the background of the Android system (services are designed to receive short messages when there is a new
*
/
Smsmanager manager=smsmanager.getdefault ();
Intent intent02=new Intent ("Com.tarena.SENT");
Pendingintent pi1=pendingintent.getbroadcast (this,0,intent02,0);
//System sometime will call the system from their own mobile phone (reached the base station) is PI1 executed
Intent intent03=new Intent ("Com.tarena.DELIVERY");
Pendingintent pi2=pendingintent.getbroadcast (this,0, intent03, 0); The
//Representative information was sent to the PI2 received on the opposite phone
Manager.sendtextmessage ("151511211", NULL, "Xihuanniyijinghenjiule", Pi1, PI2);} public class Myreciever extends broadcastreceiver{@Override public void onreceive (context context, Intent Intent) { TODO auto-generated Method Stub String action=intent.getaction (); if (("Com.tarena.SENT"). Equals (action)) {LOG.D ("TAG", "SMS sent Successfully" +system.currenttimemillis ()); } if ("Com.tarena.DELIVERY". Equals (Action)) {LOG.D ("TAG", "The other party has received a text message" +system.currenttimemillis ()); } if ("Android.provider.Telephony.SMS_RECEIVED". Equals (Action)) {//Processing short message Bundle Bundle=inten T.getextras (); Object[] pdus= (object[]) bundle.get ("PDUs"); StringBuilder sb=new StringBuilder (); String number= ""; Convert a PDUs to a segment of a short message for (int i=0;i<pdus.length;i++) {smsmessage Message=smsmessage.createfrom Pdu ((byte[]) pdus[i]); Sb.append (message.geTdisplaymessagebody ()); Number=message.getdisplayoriginatingaddress (); } log.i ("TAG", "Sender Phone:" +number+ "Send Content" +SB); There is a text message this broadcast will not continue to be issued Android.provider.Telephoney.sms_RECEIVED/* * Must set permissions Receive_sms * Set higher excellent First Class * If you do not want the system's SMS program to receive text messages, the execution of ABORTBROADCST broadcast will stop issuing the same time, * will not be written to the system database * * Abortbroad Cast (); }}} @Overrideprotected void Onresume () {super.onresume (); Receiver=new Myreciever (); Intentfilter filter=new Intentfilter (); Filter.addaction ("Com.tarena.SENT"); Filter.addaction ("Com.tarena.DELIVERY"); Filter.addaction ("Android.provider.Telephony.SMS_RECEIVED"); Filter.setpriority (1001); Registerreceiver (receiver, filter);} @Overrideprotected void OnPause () {super.onpause (); Unregisterreceiver (receiver);}
}
Declaration of Rights:
Android SMS Demo