6

I'm working on an app that will hopefully have the ability to block incoming text messages (depending on user settings), but I'm having trouble detecting incoming messages.

Would you mind looking at my codes and let me know what I'm doing wrong? I've looking through the other questions that are similar to this one but I can't find any with a detailed answer or enough information for me to reference.

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

  public class SmsReceiver extends BroadcastReceiver{

    public void onReceive(Context context, Intent intent) {
        if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
            Bundle bundle = intent.getExtras();
             if (bundle != null){
                abortBroadcast();
             }
        }
    }

  }

Here's my manifest

<receiver android:name=".listener.SmsReceiver">
 <intent-filter android:priority="100">
    <action android:name="android.provider.Telephony.SMS_RECEIVED" />
  </intent-filter>
</receiver>

I've been following the tutorial on MobiForge (http://mobiforge.com/developing/story/sms-messaging-android) as well as the questions here:

How to block an incoming message in android?

Android – Listen For Incoming SMS Messages

Can anyone point me in the right direction here? I would appreciate it.

Community
  • 1
  • 1
localhost
  • 1,062
  • 3
  • 15
  • 35
  • possible duplicate of [How to block an incoming message in android?](http://stackoverflow.com/questions/9378431/how-to-block-an-incoming-message-in-android) – t0mm13b Jun 25 '12 at 17:38
  • I've gone through that question's answers and didn't find anything helpful. – localhost Jun 25 '12 at 17:44
  • I've tried variations in my manifest receiver such as ".SmsReceiver" and ".listener.SmsReceiver". My file name is "SmsReceiver.java" – localhost Jun 25 '12 at 17:46
  • What is the problem? where is it failing? What is in the logcat? Have you stepped in the code with debugger? What is the expected output? – t0mm13b Jun 25 '12 at 17:46
  • I'm not sure where it is failing. The problem is that the incoming texts aren't blocked. I'm not sure if the receiver isn't working or the abortBroadcast(); isn't working. – localhost Jun 25 '12 at 17:48
  • I was using the RECEIVE_MMS permission in my manifest when I should have been using the RECEIVE_SMS permission. – localhost Jun 25 '12 at 20:28
  • Okie - can you please edit your question and mark it [SOLVED] in the title of the question? :) – t0mm13b Jun 25 '12 at 20:31
  • Certainly. I'm fairly new to Stack Overflow so I'm still getting a hang of how things work around here. :) – localhost Jun 25 '12 at 21:47
  • 2
    Welcome to Stack Overflow :) Be sure to check the Meta.stackoverflow.com and the guidelines to fully understand how it works :D – t0mm13b Jun 25 '12 at 21:49
  • @PeterMerrill.. +1 but if you have solved your problem then you should post as an answer so it will help other. – swiftBoy Jun 26 '12 at 05:51

1 Answers1

10

Here's what I use for blocking incoming texts. This is how I answered my question.


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
  • 1
    Hi Peter, receiving part of your code is working but sending sms is not working, i mean it delivered to the receiving end while i want to block that sms when service is running. Can you please point out what is going wrong? – Nauman Zubair May 18 '13 at 06:28
  • @NaumanZubair I actually didn't write the code to work that way. I wrote it to only block incoming texts. – localhost May 28 '13 at 14:08
  • @PeterMerrill Please guide me to achieve this gole. I'll be very thankful to you for your consideration. I am stuck here, i want to block outgoing sms. – Nauman Zubair Jul 01 '13 at 11:36
  • I don't believe that is possible. – localhost Jul 01 '13 at 15:43
  • 1
    This answer should not be accepted . i applied this and its not working. it show blocking sms but i get receive the sms. – Sagar Nayak May 07 '16 at 05:54