Points
1. Permission to "receive" SMS in Manifest.xml
<uses-permission android:name= "Android.permission.RECEIVE_SMS" ></uses-permission>
2. Register a Receive in Manifest.xml
<!--register receiver, and set priority-->
<receiver android:name= ". Autosms "android:exported=" false ">
<intent-filter android:priority=" 1000 ">
<action android: Name= "Android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
3. Define a message receipt class and rewrite the OnReceive
Inherits Broadcastreceiver public
class Autosms extends Broadcastreceiver
{
private String tag= "autsms";
Broadcast message type public
static final String sms_received_action = "Android.provider.Telephony.SMS_RECEIVED";
Overlay OnReceive Method
@Override public
void onreceive (context, Intent Intent)
{
.....
Instance
Here's the complete code:
Manifest.xml:
<manifest xmlns:android= "http://schemas.android.com/apk/res/android" package= "Com.xxh.autosms" Android: Versioncode= "1" android:versionname= "1.0" > <uses-sdk android:minsdkversion= "8" android:targetsdkv Ersion= "/>" <uses-permission android:name= "Android.permission.RECEIVE_SMS"/> <application Andr oid:icon= "@drawable/ic_launcher" android:label= "@string/app_name" android:theme= "@style/apptheme" > < Activity android:name= ". Mainactivity "android:label=" @string/title_activity_main "> <intent-filter> <action and
Roid:name= "Android.intent.action.MAIN"/> <category android:name= "Android.intent.category.LAUNCHER"/> </intent-filter> </activity> <!--register receiver, and set priority--> <receiver Android:nam E= ". Autosms "android:exported=" false "> <intent-filter android:priority=" 1000 "> <action android:nAme= "Android.provider.Telephony.SMS_RECEIVED"/> </intent-filter> </receiver> </appl
Ication> </manifest>
Autosms.java:
Package com.xxh.autosms;
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.util.Log;
Import Android.widget.Toast;
Inherits broadcastreceiver public class Autosms extends Broadcastreceiver {private String tag= "autsms";
Broadcast message type public static final String sms_received_action = "Android.provider.Telephony.SMS_RECEIVED"; Overlay OnReceive method @Override public void OnReceive (context context, Intent Intent) {//TODO auto-generated Me
Thod stub log.i (TAG, "trigger receive event");
StringBuilder body=new StringBuilder ("")//SMS Content//stringbuilder sender=new StringBuilder ("");//Sender//First Judge broadcast message
String action = Intent.getaction ();
if (Sms_received_action.equals (ACTION)) {//Get intent parameter Bundle Bundle=intent.getextras ();
Judge the content of bundle if (bundle!=null) {//Fetch PDUs content, convert to object[] object[] pdus= (object[)) bundle.get ("PDUs");
Parsing SMS smsmessage[] messages = new Smsmessage[pdus.length];
for (int i=0;i<messages.length;i++) {byte[] pdu= (byte[]) pdus[i];
MESSAGES[I]=SMSMESSAGE.CREATEFROMPDU (PDU); }//parsing the specific parameters for (Smsmessage msg:messages) {//Get SMS Contents String content
=msg.getmessagebody ();
String sender=msg.getoriginatingaddress ();
Date date = new Date (Msg.gettimestampmillis ());
SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
String sendtime = Sdf.format (date); TODO: Based on the conditions, then into the general processing if ("10060". Equals (Sender)) {//Shielding mobile phone number 10060 SMS, there are also some processing, such as the
Information sent to a third person's cell phone and so on. TODO: Test Toast.maketext (Context, "received 10060 SMS" + "content:" +content, Toast.length_long). Show ();
For specific content, cancel broadcast abortbroadcast (); else {toast.maketext (context, "Received:" +sender+ "Content:" +content+ "Time:" +sendtime.tostring (),
Toast.length_long). Show ();
}}}//if judge broadcast message end}