Skip to content Skip to sidebar Skip to footer

How To Block An Incoming Message In Android?

I am trying to develop an app in android which blocks an incoming sms. I have set the priority but its not blocking the incoming sms. I have used this.abortBroadcast() also but no

Solution 1:

Here's what I use for blocking incoming texts.


SmsReceiver.java

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;

publicclassBroadCastReceiverextendsBroadcastReceiver 
{
/** Called when the activity is first created. */privatestaticfinalStringACTION="android.provider.Telephony.SEND_SMS";
publicstaticint MSG_TPE=0;
publicvoidonReceive(Context context, Intent intent) 
{ 
    String MSG_TYPE=intent.getAction();
        if(MSG_TYPE.equals("android.provider.Telephony.SMS_RECEIVED"))
    {
//          Toast toast = Toast.makeText(context,"SMS Received: "+MSG_TYPE , Toast.LENGTH_LONG);//          toast.show();Bundlebundle= intent.getExtras();
    Object messages[] = (Object[]) bundle.get("pdus");
    SmsMessage smsMessage[] = newSmsMessage[messages.length];
    for (intn=0; n < messages.length; n++) 
    {
        smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
    }

    // show first messageToasttoast= Toast.makeText(context,"BLOCKED Received SMS: " + smsMessage[0].getMessageBody(), Toast.LENGTH_LONG);
    toast.show();
        abortBroadcast();
        for(int i=0;i<8;i++)
        {
            System.out.println("Blocking SMS **********************");
        }

    }
    elseif(MSG_TYPE.equals("android.provider.Telephony.SEND_SMS"))
    {
        Toasttoast= Toast.makeText(context,"SMS SENT: "+MSG_TYPE , Toast.LENGTH_LONG);
        toast.show();
        abortBroadcast();
        for(int i=0;i<8;i++)
        {
            System.out.println("Blocking SMS **********************");
        }

    }
    else
    {

        Toasttoast= Toast.makeText(context,"SIN ELSE: "+MSG_TYPE , Toast.LENGTH_LONG);
        toast.show();
        abortBroadcast();
        for(int i=0;i<8;i++)
        {
            System.out.println("Blocking SMS **********************");
        }

    }

}

}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?><manifestxmlns:android="http://schemas.android.com/apk/res/android"package="APP.PACKAGE.NAMEHERE"android:versionCode="1"android:versionName="1.0" ><uses-sdkandroid:minSdkVersion="10" /><supports-screensandroid:largeScreens="true"android:normalScreens="true"android:smallScreens="true"android:resizeable="true"android:anyDensity="true" /><uses-featureandroid:name="android.hardware.telephony" /><uses-permissionandroid:name="android.permission.READ_SMS" /><uses-permissionandroid:name="android.permission.WRITE_SMS" /><uses-permissionandroid:name="android.permission.SEND_SMS" /><uses-permissionandroid:name="android.permission.RECEIVE_SMS" /><uses-permissionandroid:name="android.permission.RECEIVE_MMS" /><applicationandroid:icon="@drawable/ic_launcher"android:label="@string/app_name" ><activityandroid:name=".APPACTIVITYHERE"android:label="@string/app_name"android:configChanges="orientation|keyboardHidden" ><serviceandroid:name=".MyService"android:enabled="true"/><receiverandroid:name="SmsReceiver"><intent-filterandroid:priority="2147483647"><actionandroid:name="android.provider.Telephony.SMS_SENT"/></intent-filter></receiver><serviceandroid:name=".MyServiceSentReceived"android:enabled="true"/><receiverandroid:name="SmsReceiver"><intent-filterandroid:priority="2147483645"><actionandroid:name="android.provider.Telephony.SMS_RECEIVED"/></intent-filter></receiver></application>

The main thing to take away from the manifest is the service block, receiver block, and the permissions.

Solution 2:

Add abortBroadcast(); in the if(bundle!=null){} block. that should stop it going to other apps. And I noticed that your Broadcast Receiver's name is SmsReceiver, but in Manifest, you gave it ".SMSReceiver" (case sensitive).

Solution 3:

Problem is there in your manifest, you're closing <application> tag before the receiver tag and it's wrong. All components should be inside an application tag.

Your class name is SmsReceiver, and in manifest you declared as SMSReceiver, so you won't get the broadcast at all for your receiver.

Use have to change your class name in manifest like below

<receiverandroid:name=".SmsReceiver"><intent-filterandroid:priority="99999"><actionandroid:name="android.provider.Telephony.SMS_RECEIVED" /></intent-filter></receiver></application>

And in your receiver, it's better to check the intent object for whether you got the message or not and then you can abort it.

But be careful it will abort all messages. If you want abort messages depending on some particular string you can do some manipulation on message what you got and then you can abort it.

If you want to abort all messages you can directly call abortBroadcast() before doing any manipulation on that message.

Bundleextras= intent.getExtras();        
 if ( extras != null )
    {
       // do you manipulation on String then if you can abort.if(somecondition){
       abortBroadcast();
       }
    }

Here is my manifest which working fine, once compare with your code

<?xml version="1.0" encoding="utf-8"?><manifestxmlns:android="http://schemas.android.com/apk/res/android"package="com.mypackage"android:versionCode="1"android:versionName="1.0"><uses-sdkandroid:minSdkVersion="8" /><uses-permissionandroid:name="android.permission.RECEIVE_SMS"/><uses-permissionandroid:name="android.permission.WRITE_SMS"/><uses-permissionandroid:name="android.permission.READ_SMS"/><applicationandroid:icon="@drawable/icon"android:enabled="true"android:label="@string/app_name"><serviceandroid:name="com.mypackage.service.MyService"android:exported="true"></service><receiverandroid:name="com.mypackage.receiver.MyReceiver"><intent-filterandroid:priority="100"><actionandroid:name="android.provider.Telephony.SMS_RECEIVED" /></intent-filter></receiver></application></manifest>

and the java code

publicvoidonReceive( Context context, Intent intent ) 
  {
     if(intent != null){               
         Stringaction= intent.getAction();
         if(action.equals("android.provider.Telephony.SMS_RECEIVED"))  {
                Bundleextras= intent.getExtras();    
                if ( extras != null ){
                   //read sms
                 }
            }
        }
  }

Solution 4:

@sankar

<?xml version="1.0" encoding="utf-8"?><manifestxmlns:android="http://schemas.android.com/apk/res/android"package="BVB.EDU"android:versionCode="1"android:versionName="1.0"><uses-sdkandroid:minSdkVersion="7" /><applicationandroid:icon="@drawable/icon"android:label="@string/app_name"><activityandroid:name=".SMS"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"><intent-filterandroid:priority="99999"><actionandroid:name="android.provider.Telephony.SMS_RECEIVED" /></intent-filter></receiver></application><uses-permissionandroid:name="android.permission.SEND_SMS"></uses-permission><uses-permissionandroid:name="android.permission.RECEIVE_SMS"></uses-permission></manifest>

Solution 5:

Because of the security policy in Android 4.4 and up to block the incoming SMS-messages it is required to give to the application the permissions of "Default SMS-application"

Post a Comment for "How To Block An Incoming Message In Android?"