First we need to configure some manifest files
<receiver android:name= "Com.example.smslinser.MyReceiver" >
<intent-filter android:priority= ">"
<action android:name= "Android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
Filter on behalf of SMS filter
Then also configure the priority, this range is from 1000 to 1000, we have to do interception function must ensure that we write the message recipient, before the system's message recipients receive
<uses-permission android:name= "Android.permission.RECEIVE_SMS"/>
PackageCom.example.smslinser;ImportAndroid.content.BroadcastReceiver;ImportAndroid.content.Context;Importandroid.content.Intent;ImportAndroid.os.Bundle;ImportAndroid.telephony.SmsMessage; Public classMyreceiverextendsBroadcastreceiver { PublicMyreceiver () {} @Override Public voidOnReceive (Context context, Intent Intent) {//Todo:this method is called if the Broadcastreceiver is receiving//An Intent broadcast.Bundle bundle =Intent.getextras (); //get received SMS, so designed to send a long time to receiveObject[] Object = (object[]) bundle.get ("PDUs"); for(inti = 0; i < object.length; i++) {smsmessage smsmessage= SMSMESSAGE.CREATEFROMPDU ((byte[]) object[i]); //get the sender number.String address =smsmessage.getoriginatingaddress (); //Get information ContentString BODY =Smsmessage.getmessagebody (); if(Address.equals ("138")){ //do not allow other broadcast recipients to receiveAbortbroadcast (); } System.out.println ("Address:" +address+ "body:" +body); } }}
<?XML version= "1.0" encoding= "Utf-8"?><Manifestxmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Com.example.smslinser"Android:versioncode= "1"Android:versionname= "1.0" > <USES-SDKandroid:minsdkversion= "8"android:targetsdkversion= "+" /> <uses-permissionAndroid:name= "Android.permission.RECEIVE_SMS"/> <ApplicationAndroid:allowbackup= "true"Android:icon= "@drawable/ic_launcher"Android:label= "@string/app_name"Android:theme= "@style/apptheme" > <ActivityAndroid:name= "Com.example.smslinser.MainActivity"Android:label= "@string/app_name" > <Intent-filter> <ActionAndroid:name= "Android.intent.action.MAIN" /> <categoryAndroid:name= "Android.intent.category.LAUNCHER" /> </Intent-filter> </Activity> <receiverAndroid:name= "Com.example.smslinser.MyReceiver" > <Intent-filterandroid:priority= "+"> <ActionAndroid:name= "Android.provider.Telephony.SMS_RECEIVED"/> </Intent-filter> </receiver> </Application></Manifest>
Android four components of the Broadcastreceiver SMS interception function