Android Monitoring System SMS Broadcast

Source: Internet
Author: User

Usage scenarios:

1, the system receives the text message to identify, is the advertisement or is the fraud and so on

2, to filter the content of the message or to extract the content, such as verification code extraction

3, the system SMS interception, even the system itself is not allowed to receive (will not appear in the system data, there will be no system SMS notification bar prompt)

  

Note:

1, the system SMS broadcast for the orderly broadcast, to intercept the words, need to set the broadcast priority at the time of registration is the largest, but this also has the risk, if the other application first intercepted, then we will no longer receive, use should pay attention.

2, to receive the system SMS broadcast, then the application must have SMS Read permission , which may be a limit for users

3, in addition to SMS Read permission, some phones need to have MMS Read permission (Xiaomi phone), this is a bit harsh

4, if not able to accept the 3rd, then use another way to get the text message content, that is: through the monitoring system SMS database data changes , this separate introduction

5, the system SMS database is also by listening to SMS broadcast way to get SMS content data, but the system of their own things it has the default permission to allow, not to worry because the permissions issue cannot receive SMS broadcast

5th, you can verify this: write a message to the receiver, the message broadcast to intercept, you will find the system itself can not receive the text message content.

If it is the above several scenes you are through the listening system SMS broadcast, and then parse out the contents of the system text messages, and then other related processing of SMS content, monitoring system SMS broadcast code as follows:

Private Static classSmsreceiverextendsbroadcastreceiver {smsreceiverprocessor msmsreceiverprocessor; Smsreceiver () {msmsreceiverprocessor=Newsmsreceiverprocessor (); } @Override Public voidOnReceive (Context context, Intent Intent) {if(Intent = =NULL) {            return; } String Action=intent.getaction (); if(SmsReceiverProcessor.ANDROID_PROVIDER_TELEPHONY_SMS_RECEIVED.equals (action)||SmsReceiverProcessor.ANDROID_PROVIDER_TELEPHONY_SMS_RECEIVED2.equals (Action)||SmsReceiverProcessor.ANDROID_PROVIDER_TELEPHONY_SMS_RECEIVED_2.equals (Action)||SmsReceiverProcessor.ANDROID_PROVIDER_TELEPHONY_GSM_SMS_RECEIVED.equals (Action))        {msmsreceiverprocessor.handlesms (intent); }        //If you need to intercept the broadcast, call the following statementAbortbroadcast (); }} Public classSmsreceiverprocessor { Public Static FinalString android_provider_telephony_sms_received = "Android.provider.Telephony.SMS_RECEIVED";  Public Static FinalString android_provider_telephony_sms_received2 = "Android.provider.Telephony.SMS_RECEIVED2";  Public Static FinalString android_provider_telephony_sms_received_2 = "Android.provider.Telephony.SMS_RECEIVED_2";  Public Static FinalString android_provider_telephony_gsm_sms_received = "Android.provider.Telephony.GSM_SMS_RECEIVED";  Publicsmsreceiverprocessor () {} Public voidhandlesms (Intent Intent) {smsmessage[] SMSs=smsutils.getmessagesfromintent (Intent); if(SMSs! =NULL&& smss.length >= 1) {StringBuilder bodybuf=NewStringBuilder (); String PhoneNumber= "";//Phone number            LongTime = 0;  for(Smsmessage msg:smss) {Try{bodybuf.append (Msg.getdisplaymessagebody ()); PhoneNumber=msg.getdisplayoriginatingaddress (); time=Msg.gettimestampmillis (); } Catch(Exception e) {e.printstacktrace (); Continue; }            }            FinalString smscontent = bodybuf.tostring ();//SMS Content            if(Textutils.isempty (phonenumber) | |Textutils.isempty (smscontent)) {                return; }            //get SMS numbers and content to be processedSystem.out.println ("PhoneNumber:" + PhoneNumber + "Smscontent:" +smscontent); }    }}//Smsutils.java Code Public classSmsutils { Public Staticsmsmessage[] Getmessagesfromintent (Intent Intent) {//Moto's dual-mode mobile phone        if(Ismototwomode ()) {returngetMessagesFromIntentInMotoXT800 (Intent); } object[] Messages= (object[]) Intent.getserializableextra ("PDUs")); if(Messages = =NULL) {            return NULL; }        Final intPducount =messages.length; Smsmessage[] Msgs=NewSmsmessage[pducount]; Try {             for(inti = 0; i < Pducount; i++) {Msgs[i]= SMSMESSAGE.CREATEFROMPDU ((byte[]) messages[i]); }        } Catch(Throwable e) {return NULL; }        returnmsgs; }    Private Staticsmsmessage[] getMessagesFromIntentInMotoXT800 (Intent Intent) {String strfrom= Intent.getstringextra ("from"); BooleanBCDMA; if(Strfrom = =NULL)            return NULL; if(Strfrom.equals ("GSM") ) {BCDMA=false; } Else if(Strfrom.equals ("CDMA") ) {BCDMA=true; } Else {            return NULL; } object[] Messages= (object[]) Intent.getserializableextra ("PDUs")); byte[] Pduobjs =New byte[messages.length][];  for(inti = 0; i < messages.length; i++) {Pduobjs[i]= (byte[]) messages[i]; }        byte[] PDUs =New byte[pduobjs.length][]; intPducount =pdus.length; Smsmessage[] Msgs=NewSmsmessage[pducount];  for(inti = 0; i < Pducount; i++) {Pdus[i]=Pduobjs[i]; Smsmessagebase obj=XT800CREATEFROMPDU (Pdus[i], BCDMA); Try{Msgs[i]= Smsmessage.class. newinstance (); Field F= Smsmessage.class. GetField ("Mwrappedsmsmessage");            F.set (Msgs[i], obj); } Catch(Exception e) {e.printstacktrace (); }        }        returnmsgs; }    //MOTO xt800 above the SMS parsing    Private StaticSmsmessagebase XT800CREATEFROMPDU (byte[] PDU,BooleanBCDMA) {smsmessagebase Wrappedmessage=NULL; if(BCDMA) {wrappedmessage=Com.android.internal.telephony.cdma.SmsMessage.createFromPdu (PDU); } Else{wrappedmessage=Com.android.internal.telephony.gsm.SmsMessage.createFromPdu (PDU); }        returnWrappedmessage; }    //a dual-mode mobile phone that determines whether it is a motorcycle     Public Static BooleanIsmototwomode () {FinalString strXT800 = "XT800"; FinalString strxt800plus = "xt800+"; FinalString strXT806 = "XT806"; FinalString strXT882 = "XT882"; String Model=Build.model; if(Model! =NULL) {String upper=model.touppercase (); if(Upper.equals (strXT800) | |upper.equals (Strxt800plus)|| Upper.equals (strXT806) | |upper.equals (strXT882)) {                return true; }        }        return false; }}
the above code needs to import two classes as follows: Import Android.telephony.SmsMessage; import com.android.internal.telephony.SmsMessageBase;

Next is the registration of the broadcast, which uses dynamic registration, broadcast registration and anti-registration combined activity or service life cycle to use, specifically no longer detailed:

Private StaticBroadcastreceiver Msmsreceiver =NULL;Private Static voidregistersmsreceiver (Contextwrapper contextwrapper) {Try{msmsreceiver=NewSmsreceiver (); Intentfilter Filter=NewIntentfilter (smsreceiverprocessor.android_provider_telephony_sms_received);        Filter.addaction (SMSRECEIVERPROCESSOR.ANDROID_PROVIDER_TELEPHONY_SMS_RECEIVED2);        Filter.addaction (smsreceiverprocessor.android_provider_telephony_sms_received_2);        Filter.addaction (smsreceiverprocessor.android_provider_telephony_gsm_sms_received);   Filter.setpriority (Integer.max_value); //here Although this is the maximum value for integers, but should actually not allow more than 1000 of the system to run, no authenticationContextwrapper.registerreceiver (Msmsreceiver, filter, Manifest.permission.BROADCAST_SMS,NULL); } Catch(Throwable e) {    }}Private Static voidunregistersmsreceiver (Contextwrapper contextwrapper) {Try{contextwrapper.unregisterreceiver (msmsreceiver); } Catch(Exception e) {    }}

If it is a simple application, the use of the above method to obtain the text content can meet the needs, but if the demand for coverage requirements may not be high, especially for MMS permissions or other permissions to rely on is very inconvenient, so most of the time using the monitoring system SMS database content changes in the way to obtain the text message content.

Android Monitoring System SMS Broadcast

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.