Question

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>
Was it helpful?

Solution

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

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top