2

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 result.

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

import android.widget.Toast;

@SuppressWarnings("deprecation")
public class SmsReceiver extends BroadcastReceiver
{

@Override
public void onReceive(Context context, Intent intent) 
{
this.abortBroadcast();  

    Bundle bundle = intent.getExtras();        
    SmsMessage[] msgs = null;
    String str = "";

    if (bundle != null)
    {


        Object[] pdus = (Object[]) bundle.get("pdus");
        msgs = new SmsMessage[pdus.length];            
        for (int i=0; i<msgs.length; i++){
            msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);                
            str += "SMS from " + msgs[i].getOriginatingAddress();                     
            str += " :";
            str += msgs[i].getMessageBody().toString();
            str += "\n";        

}


}
}
}

and the manifest file is like this

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="BVB.EDU"
  android:versionCode="1"
  android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" />

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".SMS"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

</application>

<receiver android:name=".SMSReceiver">
     <intent-filter android:priority="99999">
         <action android:name="android.provider.Telephony.SMS_RECEIVED" />                  
     </intent-filter>
 </receiver>

<uses-permission android:name="android.permission.SEND_SMS">
</uses-permission>
<uses-permission android:name="android.permission.RECEIVE_SMS">
</uses-permission>

</manifest>
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Akarsh_Shetty
  • 47
  • 1
  • 1
  • 5

5 Answers5

2

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;

public class BroadCastReceiver extends BroadcastReceiver 
{
/** Called when the activity is first created. */
private static final String ACTION = "android.provider.Telephony.SEND_SMS";
public static int MSG_TPE=0;
public void onReceive(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();

    Bundle bundle = intent.getExtras();
    Object messages[] = (Object[]) bundle.get("pdus");
    SmsMessage smsMessage[] = new SmsMessage[messages.length];
    for (int n = 0; n < messages.length; n++) 
    {
        smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
    }

    // show first message
    Toast toast = 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 **********************");
        }

    }
    else if(MSG_TYPE.equals("android.provider.Telephony.SEND_SMS"))
    {
        Toast toast = 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
    {

        Toast toast = 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"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="APP.PACKAGE.NAMEHERE"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="10" />

<supports-screens 
android:largeScreens="true" 
android:normalScreens="true" 
android:smallScreens="true" 
android:resizeable="true" 
android:anyDensity="true" />

<uses-feature android:name="android.hardware.telephony" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.RECEIVE_MMS" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".APPACTIVITYHERE"
        android:label="@string/app_name"
        android:configChanges="orientation|keyboardHidden" >


    <service android:name=".MyService" android:enabled="true"/>
     <receiver android:name="SmsReceiver">
      <intent-filter android:priority="2147483647">
       <action android:name="android.provider.Telephony.SMS_SENT"/>
      </intent-filter>
     </receiver>

     <service android:name=".MyServiceSentReceived" android:enabled="true"/>
      <receiver android:name="SmsReceiver">
        <intent-filter android:priority="2147483645">
         <action android: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.

localhost
  • 1,062
  • 3
  • 15
  • 35
  • You may abortBroadCast() before handling intent. I put it on top of onReceive() because everything I do will work the same (intent stays there), but I am certain that, no matter how long my onReceive manages to work before being killed, broadcast indeed will be aborted. – Budimir Grom Oct 25 '14 at 09:50
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).

Seshu Vinay
  • 13,560
  • 9
  • 60
  • 109
2

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

 <receiver android:name=".SmsReceiver">
         <intent-filter android:priority="99999">
             <action android: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.

Bundle extras = 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"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.mypackage"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />    
    <uses-permission android:name="android.permission.RECEIVE_SMS"/>
    <uses-permission android:name="android.permission.WRITE_SMS"/>
    <uses-permission android:name="android.permission.READ_SMS"/>
    <application android:icon="@drawable/icon" android:enabled="true" android:label="@string/app_name">
        <service android:name="com.mypackage.service.MyService" android:exported="true">
        </service>
        <receiver android:name="com.mypackage.receiver.MyReceiver">        
        <intent-filter android:priority="100"><action android:name="android.provider.Telephony.SMS_RECEIVED" /> </intent-filter> 
        </receiver>
    </application>
</manifest>

and the java code

public void onReceive( Context context, Intent intent ) 
  {
     if(intent != null){               
         String action = intent.getAction();
         if(action.equals("android.provider.Telephony.SMS_RECEIVED"))  {
                Bundle extras = intent.getExtras();    
                if ( extras != null ){
                   //read sms
                 }
            }
        }
  }
Sankar
  • 1,685
  • 2
  • 16
  • 27
  • If still you didn't receive any broadcast, once check package name where your receiver is there and if it differ from main package name what you declared in manifest then mention full class name with package for your receiver in manifest. – Sankar Feb 22 '12 at 05:39
  • If it not works after moving application tag also, please post your new manifest with these changes. – Sankar Feb 22 '12 at 13:43
  • @sankar Broadcast is received but still it is showing in default SMS app. – vijaypalod Jan 06 '18 at 10:06
0

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"

Mohammad Zaer
  • 638
  • 7
  • 9
0

@sankar

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="BVB.EDU"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".SMS"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".SmsReceiver">
<intent-filter android:priority="99999">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />                  
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.SEND_SMS">
</uses-permission>
<uses-permission android:name="android.permission.RECEIVE_SMS">
</uses-permission>   
</manifest>
Akarsh_Shetty
  • 47
  • 1
  • 1
  • 5
  • i edited my answer by posting my code which working for me, once check that and compare with your code. – Sankar Feb 22 '12 at 15:08