Recently done in the project to listen to the phone's short message content, this thing seems to have done at the beginning of the study, but now forget, now write down, remember.
Prerequisites-Permissions:
<android:name= "Android.permission.RECEIVE_SMS"> </ uses-permission> <android:name= " Android.permission.READ_SMS "> </uses-permission>
Mode one: Listen to SMS messages by radio
(Note: This method only works for newly received short messages, runs the code, does not read the read or unread messages in the Inbox, and only executes the onchange method if a new short message is received)
Register a broadcast:
<receiverAndroid:name= "Com.dbjtech.acbxt.waiqin.SmsReciver" > <Intent-filterandroid:priority= "999"> <ActionAndroid:name= "Android.provider.Telephony.SMS_RECEIVED" /> </Intent-filter> </receiver>
Smsreceiver.java
1 Public classSmsreciverextendsBroadcastreceiver {2 3 @Override4 Public voidOnReceive (Context context, Intent Intent) {5Bundle bundle =Intent.getextras ();6Smsmessage msg =NULL;7 if(NULL!=bundle) {8Object[] Smsobj = (object[]) bundle.get ("PDUs");9 for(Object object:smsobj) {Tenmsg = SMSMESSAGE.CREATEFROMPDU ((byte[]) object); OneDate Date =NewDate (Msg.gettimestampmillis ());//Time ASimpleDateFormat format =NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); -String Receivetime =Format.format (date); -System.out.println ("Number:" +msg.getoriginatingaddress () the+ "Body:" + msg.getdisplaymessagebody () + "Time:" -+Msg.gettimestampmillis ()); - - //to write your own logic here. + if(Msg.getoriginatingaddress (). Equals ("10086")) { - //TODO + A } at - } - } - } - -}
This broadcast of the system is an orderly broadcast, that is, when other programs first get the broadcast to pass to you, of course, it can also kill the broadcast, so that you do not receive, so your program must not receive this broadcast. We found that by setting the value of priority, in fact, sometimes it doesn't work, and now in some custom-made systems or with security software, often short messages are intercepted and killed.
Then, we can only use method two, listen to the change of SMS database, this way is slightly more complicated than the method, but it is very convenient to use, not affected by other programs ~
Method Two: Get SMS content via SMS database
(Note: This way can get all the text messages on the phone, including read unread text messages, is not great)
1 PrivateUri Sms_inbox = Uri.parse ("content://sms/");2 Public voidGetsmsfromphone () {3Contentresolver CR =getcontentresolver ();4string[] Projection =NewString[] {"Body"};//"_id", "address", "person", "date", "Type "5String where = "address = ' 1066321332 ' and date >"6+ (System.currenttimemillis ()-10 * 60 * 1000);7Cursor cur = cr.query (sms_inbox, projection, where,NULL, "Date desc");8 if(NULL==cur)9 return;Ten if(Cur.movetonext ()) { OneString number = cur.getstring (Cur.getcolumnindex ("Address"));//Phone number AString name = cur.getstring (Cur.getcolumnindex ("person"));//Contact person Name List -String BODY = cur.getstring (Cur.getcolumnindex ("Body")); - //here I'm going to get the verification code in my SMS service number ~ ~ thePattern 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); - } + } A}
SMS Main structure:
_ID: SMS number, such as 100
THREAD_ID: The serial number of the conversation, such as 100, with the same mobile phone number of each other SMS, its serial number is the same
Address: The sender's location, which is the phone number, such as +86138138000
Person: sender, if the sender is a specific name in the Address book, The Stranger is null
Date: Dates, long type, such as 1346988516, can be set for date display format
Protocol: Protocol 0sms_rpoto SMS, 1mms_proto MMS
READ: Reading 0 unread, 1 read
Status: SMS state-1 Receive, 0complete,64pending,128failed
Type: SMS Type 1 is received, 2 is issued
Body: Text Message specific content
Service_center: SMS Service center number, such as +8613800755500
By way of two, we can get all the short messages in the phone, but there is a problem, if a new short message? We can never write a thread, how many seconds, to read the SMS database it? In fact, we can put the way two different ways to write:
PrivateSmsobserver Smsobserver; protected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.app_login); Smsobserver=NewSmsobserver ( This, Smshandler); Getcontentresolver (). Registercontentobserver (Sms_inbox,true, Smsobserver); } PublicHandler Smshandler =NewHandler () {//This is where callbacks can be done.//TODO }; classSmsobserverextendsContentobserver { PublicSmsobserver (Context context, Handler Handler) {Super(handler); } @Override Public voidOnChange (BooleanSelfchange) { Super. OnChange (Selfchange); //whenever a new message arrives, use our method of getting a short messageGetsmsfromphone (); } }
Original address: http://blog.csdn.net/mad1989/article/details/22426415
Android Development monitor or get SMS content "Turn"