This article introduced the Android SMS and broadcast receiver to implement SMS listening, pay attention to the Android list of permissions and broadcast registration monitoring implementation, nonsense said, the code is as follows:
The following is the XML for the Android manifest
Androidmanifest.xml
<manifest xmlns:android= "http://schemas.android.com/apk/res/android" package= "Com.zyw.broadcastsendsms" Android:versioncode= "1" android:versionname= "1.0" > <uses-sdk android:minsdkversion= "8" android:targets
Dkversion= "/>" <application android:icon= "@drawable/ic_launcher" android:label= "@string/app_name" Android:theme= "@style/apptheme" > <activity android:name= ". Sendsms "android:label=" @string/title_activity_send_sms "> <intent-filter> <action Android
: Name= "Android.intent.action.MAIN"/> <category android:name= "Android.intent.category.LAUNCHER"/> </intent-filter> </activity> <receiver android:name= ". Smsbroadcastreceiver "> <intent-filter android:priority=" 1000 "> <action android:name=" Android.pro Vider. Telephony.sms_received "/> </intent-filter> </receiver> </application> &lT;uses-permission android:name= "Android.permission.SEND_SMS" ></uses-permission><!--Add Permissions--> < Uses-permission android:name= "Android.permission.RECEIVE_SMS" ></uses-permission> <uses-permission
Android:name= "Android.permission.READ_SMS" ></uses-permission> </manifest>
Send the primary interface Mian.xml and implement activity Sendsms.java
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/" Android "Android:layout_width=" Fill_parent "android:layout_height=" fill_parent "android:orientation=" "vertical" and
roid:padding= "10SP" > <textview android:layout_width= "fill_parent" android:layout_height= "Wrap_content" android:text= "mobile number"/> <edittext android:id= "@+id/number" android:numeric= "integer" Android:layout _width= "Fill_parent" android:layout_height= "wrap_content" android:hint= "Please enter phone number"/> <textview Android : layout_width= "fill_parent" android:layout_height= "wrap_content" android:text= "SMS Content"/> <EditText and Roid:id= "@+id/content" android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:hint= "please Enter SMS Content "android:lines=" 3 "/> <textview android:layout_width=" fill_parent "android:layout_height=" wrap _content "> </textview>
<button android:id= "@+id/btnsend" android:layout_width= "fill_parent" android:layout_height= "Wrap_content"
android:gravity= "center" android:paddingtop= "20SP" android:text= "Send text message" android:onclick= "Send"/>
</LinearLayout>
Implement activity
Sendsms.java
Package com.zyw.broadcastsendsms;
Import java.util.ArrayList;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.telephony.SmsManager;
Import Android.view.View;
Import Android.widget.EditText;
Import Android.widget.Toast;
public class Sendsms extends activity{private edittext num;
Private EditText content;
@Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Num= (EditText) Findviewbyid (R.id.number);
Content= (EditText) Findviewbyid (r.id.content);
public void Send (view view) {String strno=num.gettext (). toString ();
String Strcontent=content.gettext (). toString ();
Smsmanager Smsmanager = Smsmanager.getdefault (); If the word number is more than 5, split into more than one SMS send if (Strcontent.length () > 5) {arraylist<string> msgs = Smsmanager.dividemessage (s
Trcontent); for (String msg:msgs) {smsmanager.sendtextmessage (Strno, NULL, MSG, NULL, nulL);
} else {smsmanager.sendtextmessage (Strno, NULL, strcontent, NULL, NULL);
} num.settext ("");
Content.settext ("");
Toast.maketext (sendsms.this, "SMS Send Complete", Toast.length_long). Show ();
}
}
Broadcast receivers implement SMS listening Smsbroadcastreceiver.java
Package com.zyw.broadcastsendsms;
Import Java.text.SimpleDateFormat;
Import Java.util.Date;
Import Android.content.BroadcastReceiver;
Import Android.content.Context;
Import android.content.Intent;
Import Android.os.Bundle;
Import Android.telephony.SmsMessage;
Import Android.widget.Toast;
public class Smsbroadcastreceiver extends Broadcastreceiver {public void OnReceive (context context, Intent Intent) {
Smsmessage msg = NULL;
Bundle Bundle = Intent.getextras ();
if (bundle!= null) {object[] pdusobj = (object[)) bundle.get ("PDUs");
for (Object p:pdusobj) {msg= SMSMESSAGE.CREATEFROMPDU ((byte[)) p);
String msgtxt =msg.getmessagebody ()//Gets the content of the message date date = new Date (Msg.gettimestampmillis ());/Time
SimpleDateFormat format = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
String receivetime = Format.format (date);
String Sendernumber = msg.getoriginatingaddress ();
if (Msgtxt.equals ("testing!"))
{Toast.maketext (context, "success!", Toast.length_long). Show ();
System.out.println ("success!");
Return
else {toast.maketext (context, MsgTxt, Toast.length_long). Show ();
System.out.println ("Sender:" +sendernumber+ "SMS Content:" +msgtxt+ "Accept the Time:" +receivetime);
Return
} return;
}
}
}
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.