2

i developing an application where i want to block SMS of some specific numbers.For testing i do SMS from that number that i have in IF CONDITION but this code is not blocking that number SMS. i try best but did't resolved. any one help me.Here is my code
Sms.java

public class Sms extends BroadcastReceiver {
            final SmsManager sms = SmsManager.getDefault();

            @Override
            public void onReceive(Context context, Intent intent) {

                // Retrieves a map of extended data from the intent.
                final Bundle bundle = intent.getExtras();

                try {

                    if (bundle != null) {

                        final Object[] pdusObj = (Object[]) bundle.get("pdus");

                        for (int i = 0; i < pdusObj.length; i++) {

                            SmsMessage currentMessage = SmsMessage
                                    .createFromPdu((byte[]) pdusObj[i]);
                            String phoneNumber = currentMessage
                                    .getDisplayOriginatingAddress();

                            String senderNum = phoneNumber;
                            String message = currentMessage.getDisplayMessageBody();

                            Log.i("SmsReceiver", "senderNum: " + senderNum
                                    + "; message: " + message);


                            // Show Alert
                            int duration = Toast.LENGTH_SHORT;
                            Toast toast = Toast.makeText(context, "senderNum: "
                                    + senderNum + ", message: " + message, duration);
                            toast.show();
                            if(senderNum=="+923215619915"){
                                Toast.makeText(context, "You r in if condition", Toast.LENGTH_LONG).show();
                                abortBroadcast();
                            }



                        } // end for loop
                    } // bundle is null

                } catch (Exception e) {
                    Log.e("SmsReceiver", "Exception smsReceiver" + e);

                }
            }
        } 

Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.tesingworkspace"
        android:versionCode="1"
        android:versionName="1.0" >

        <uses-sdk android:minSdkVersion="7" 
             android:targetSdkVersion="19"
           />
         <uses-permission android:name="android.permission.SEND_SMS" >
        </uses-permission>
       <uses-permission android:name="android.permission.READ_CONTACTS" />
        <uses-permission android:name="android.permission.RECEIVE_SMS" >
        </uses-permission>
        <uses-permission android:name="android.permission.READ_SMS"/>

        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="com.example.tesingworkspace.BroadCastSms"
                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="com.example.tesingworkspace.Sms">   
                <intent-filter>
                    <action android:name="android.provider.Telephony.SMS_RECEIVED" />
                </intent-filter>
            </receiver>

        </application>



    </manifest>
Zia Ur Rehman
  • 129
  • 2
  • 13

2 Answers2

3

The best thing you need to do is debug your code, you have hard coded the phone number in a format that may or may not occur, make sure in what format your receiver phone number appears? When I was working over it I was sending message over 0333xxx-xxxx number and I was receiving as +92xxx. But it's not sure for all, different telecommunication companies may use different format, for that you should use

if(number.equals("+92333xxx-xxxxx")) or better use a contains, that would actually make it more appropriate to match the number and remove the possibility of format error

if(number.contains("333xxx-xxxxx")){
    // Your code to abort message
}

Hope this helps

Saqib
  • 1,120
  • 5
  • 22
  • 40
  • I checked that code but did't solve a issue.. Is there a issue in code to abort message ? – Zia Ur Rehman Mar 28 '14 at 06:04
  • have you debug that? what number are you getting? when received message? – Saqib Mar 28 '14 at 06:14
  • another thing I have seen in your code is, you are aborting message in side the for loop. Please put the abort message code outside the loop. – Saqib Mar 28 '14 at 06:20
  • 1
    I got your issue, the reason is you checking number inside the for loop, what actually happens is your number is in parts for the time being, which is being collected inside the loop, so on each part you are matching whether it's the same as your number or not, and ofcourse it won't match! Put the part of checking your number outside the for loop and see it working... – Saqib Mar 28 '14 at 06:51
  • Thanks Sir, Problem Solved and now sms is not show but notification is display on the receiver side , how will this control? – Zia Ur Rehman Mar 28 '14 at 06:57
  • 1
    try to add priority to your SMS_RECEIVED broadcast so that it comes to your broadcast first than others `` – Saqib Mar 28 '14 at 07:01
1

Equals used for comparing the object only. try to use if(senderNum.equalsIngnorecase("+923215619915")) or if(senderNum.indexOf("+923215619915") != -1).

Yuvaraja
  • 715
  • 6
  • 22