The first is through Broadcastreciver
<uses-permission android:name= "Android.permission.RECEIVE_SMS"/> <!--intercept SMS (that is, accept shorter privileges)--<receiver Android:name= "Com.demo.artshell.broadcastreceiverdemo.InterceptReceiver" > <!--not set up Android here: Enable and Android:exported properties, <a href= "http://developer.android.com/guide/topics/manifest/receiver-element.html" target= "_blank" rel= "nofollow" > Reference official Documents </a>--> <!--The priority here is 1000, assuming your phone does not have other third-party SMS blocker software, such as 360, Otherwise, other third-party software blocking permissions too high cause you to intercept SMS--<intent-filter android:priority= "> <action android:name= "Android.provider.Telephony.SMS_RECEIVED"/> </intent-filter></receiver
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; import java.text.simpledateformat;import java.util.date; public class interceptreceiver extends broadcastreceiver { public static final String TAG = "Interceptreceiver"; public static final String ACTION_SMS_RECEIVED = " Android.provider.Telephony.SMS_RECEIVED "; public interceptreceiver () {} @Override public void onreceive (context context, intent intent) { log.i (TAG, "----------- ----Interceptreceiver onreceive ()----------------"); if (Action_sms_received.equals ( Intent.getaction ())) { bundle carrycontent = intent.getextras (); if (carrycontent != null) { stringbuilder sb = new stringbuilder (); // Get all the text messages you receive via PDUs, get SMS content Object[] pdus = (object[]) carrycontent.get ("PDUs"); // build an array of SMS objects SmsMessage[] mges = new SmsMessage[pdus.length]; for (int i = 0, len = pdus.length; i < len; i++) { // Get a single SMS content, save in PDU format, and generate SMS Objects &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;MGES[I]&NBSP;=&NBSP;SMSMESSAGE.CREATEFROMPDU ((byte[]) pdus[i]); } for (smsmessage mge : mges) { &nbsP; sb.append ("SMS From:" ). Append (Mge.getdisplayoriginatingaddress ()). Append ("\ n") .append ("Message content:"). Append (Mge.getmessagebody ()). Append ("\ n"); date senddate = new date (Mge.gettimestampmillis ()); simpledateformat format = new simpledateformat ("Yyyy-mm-dd hh:mm:ss"); sb.append ("SMS Send Time:"). Append ( Format.format (senddate)); } &nbSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;LOG.I (TAG,sb.toString ()); // Print log records Toast.maketext (Context,sb.tostring (), Toast.length_long). Show (); this.abortbroadcast (); // no longer spreading down } } }}
The second way through Contentobserver
Content://sms/inbox Inbox
Content://sms/sent has been sent
Content://sms/draft Draft
Content://sms/outbox Outbox
content://sms/failed Send failed
content://sms/queued to send list
Private smsobserver smsobserver;protected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.app_login); smsobserver = new Smsobserver (this, null); Getcontentresolver (). Registercontentobserver (Sms_inbox, true,smsobserver);} protected void OnStop () {super.onstop (); Registration must be de-registered, otherwise activity cannot destroy Getcontentresolver (). Unregistercontentobserver (Smsobserver);} Class Smsobserver extends Contentobserver {public Smsobserver (context context, Handler Handler) {super (Handler);} @Overridepublic void OnChange (Boolean selfchange) {Super.onchange (selfchange);//Whenever a new message arrives, Use our method of getting short messages Getsmsfromphone ();}}
Querying the database
private uri sms_inbox = uri.parse ("Content ://sms/inbox ");p Ublic void getsmsfromphone () {ContentResolver cr = Getcontentresolver (); string[] projection = new string[] { "Body" };//"_id", "Address", "Person",, "date", "typestring where = " address = ' 1066321332 ' AND date > "+ (System.currenttimemillis () - 10 * 60 * 1000); Cursor cur = cr.query (sms_inbox, projection, where, null, "Date desc") ;if (null == cur) return;if (Cur.movetonext ()) {String number = Cur.getstring (Cur.getcolumnindex ("Address"));//Mobile number string name = cur.getstring ( Cur.getcolumnindex ("person"));//Contact Name list string body = cur.getstring (Cur.getcolumnindex ("Body")) and/or here I'm going to get my own SMS service numberThe verification code in ~~pattern pattern = pattern.compile (" [a-za-z0-9]{10}"); Matcher matcher = pattern.matcher (body);if (Matcher.find ()) {string res = matcher.group (). substring (1, 11); Mobiletext.settext (res);}}
SMS Main structure:
_ID: SMS serial number, such as thread_id: the serial number of the dialogue, such as 100, with the same phone number of each other SMS, its serial number is the same address: the sender, that is, the mobile phone number, such as +86138138000 person: sender, if the sender in the Address book is a Body name, stranger null Date: Date, long type, such as 1346988516, can be set for date display format Protocol: Protocol 0sms_rpoto SMS, 1mms_proto MMS read: Read 0 unread, 1 read STA TUS: SMS Status-1 Receive, 0complete,64pending,128failed type: SMS Type 1 is received, 2 is issued body: SMS Specific content service_center: SMS Service center number, such as +86138007 55500
Android4.4 Some questions, look here.
How to implement SMS Blocker on Android 4.4
------------------------------------------------
Recommended Blog:
http://my.csdn.net/L173864930
http://su1216.iteye.com/
2 ways to intercept Android SMS