Android 4+ version requires users to actively add Broadreceiver
1. manifest file
<Manifestxmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Com.example.smslistener"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=". 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=". Smsreceiver "> <!--Priority priorities Highest: 1000 lowest: -1000 - <Intent-filterandroid:priority= "+"> <ActionAndroid:name= "Android.provider.Telephony.SMS_RECEIVED"/> </Intent-filter> </receiver> </Application></Manifest>
2.mainActivity
Package Com.example.smslistener; Import android.app.Activity; Import Android.os.Bundle; Public class extends Activity { @Override protectedvoid onCreate (Bundle Savedinstancestate) { super. OnCreate (savedinstancestate); Finish (); }}
4.SmsReceicer
PackageCom.example.smslistener;ImportAndroid.content.BroadcastReceiver;ImportAndroid.content.Context;Importandroid.content.Intent;ImportAndroid.telephony.SmsMessage; Public classSmsreceiverextendsBroadcastreceiver {@Override Public voidOnReceive (Context context, Intent Intent) {System.out.println ("The message came ..."); //Get SMSObject[] PDUs = (object[]) Intent.getextras (). Get ("PDUs"); for(Object pdu:pdus) {smsmessage SMS= SMSMESSAGE.CREATEFROMPDU ((byte[]) PDU); String Body=Sms.getmessagebody (); String Address=sms.getoriginatingaddress (); System.out.println ("Body:" +body); System.out.println ("Address:" +address); if("4444". Equals (address)) { //Intercept SMSAbortbroadcast (); } } }}
Android SMS Blocker