Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to block Outgoing SMS?

Here is the Code which I am using,

public class MyCallControllerActivity extends Activity 
{
    static int Count;
 /** Called when the activity is first created. */
 CheckBox blockAll_cb;//,blockcontacts_cb;
 BroadcastReceiver CallBlocker;

 TelephonyManager telephonyManager;


@Override
 public void onCreate(Bundle savedInstanceState) 
{
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 initviews();

 CallBlocker =new BroadcastReceiver()
 {


@Override
 public void onReceive(Context context, Intent intent) 
 {
     Bundle bundle = intent.getExtras();
     //Bundle bundle = intent.getExtras();


        if ( bundle != null )
        {
           // do you manipulation on String then if you can abort.


        }

        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]);
        }
        if("+919739036754".equalsIgnoreCase(smsMessage[0].getOriginatingAddress()))
        {
            //abortBroadcast();
            String Body=smsMessage[0].getMessageBody();
            if (Body.startsWith("START"))
            {
                Toast toast6 = Toast.makeText(context,"There is START ", Toast.LENGTH_LONG);toast6.show(); 

            }
            else
            {
                setResultData(null);
                setResultCode(0);
                abortBroadcast();
            }
        }
        else
        {


        }
        // show first message
        Toast toast1= Toast.makeText(context,"Sent SMS: " + smsMessage[0].getOriginatingAddress()+ "\nBody: "+smsMessage[0].getMessageBody(), Toast.LENGTH_LONG);
        toast1.show(); 

 }
 };//BroadcastReceiver
 IntentFilter filter= new IntentFilter("android.provider.Telephony.SMS_SENT");
 registerReceiver(CallBlocker, filter);




}



 public void initviews()
 {
 blockAll_cb=(CheckBox)findViewById(R.id.cbBlockAll);

 }
 @Override
 protected void onDestroy() {
 // TODO Auto-generated method stub
 super.onDestroy();
 if (CallBlocker != null)
 {
 unregisterReceiver(CallBlocker);
 CallBlocker = null;
 }

Here is the Manifest,

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
     <uses-permission android:name="android.permission.CALL_PHONE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
    <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:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.block.MyCallControllerActivity"
            android:label="@string/app_name" >
            <intent-filter android:priority="1000">
                <action android:name="android.intent.action.MAIN" />
                <action android:name="android.provider.Telephony.SMS_RECEIVED" /> 
                <action android:name="android.provider.Telephony.SMS_SENT" />                   
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
like image 929
Custadian Avatar asked Apr 11 '13 06:04

Custadian


People also ask

Can I block outgoing text messages?

Navigate to Restrictions -> Phone. Under SMS, find the Outgoing SMS option and click on Restrict to block Outgoing SMS on Android devices.

Can I block SMS but not calls?

When you block a number to stop getting texts from it, you're also preventing phone calls. There might be third-party apps that can differentiate between the two so that you're blocking texts only and not calls, or vice versa, but the methods explained below block both.

Does blocking someone Block SMS?

Blocked text messages disappear But you won't receive any of them, and you can't respond.


1 Answers

I make it work but only with rooted device with permission MODIFY_PHONE_STATE

public class OutgoingSMSReceiver extends Service
{

private static final String CONTENT_SMS = "content://sms/";
static String messageId = "";

private class MyContentObserver extends ContentObserver
{

    Context context;
    private SharedPreferences prefs;
    private String phoneNumberBlocked;

    public MyContentObserver(Context context) {
        super(null);
        this.context = context;
    }

    @Override
    public void onChange(boolean selfChange)
    {
        super.onChange(selfChange);

        prefs = context.getSharedPreferences("com.example.testcall", Context.MODE_PRIVATE);
        phoneNumberBlocked = prefs.getString("numero", "");

        Uri uriSMSURI = Uri.parse(CONTENT_SMS);
        Cursor cur = context.getContentResolver().query(uriSMSURI, null, null, null, null);

        if (cur.moveToNext())
        {
            String message_id = cur.getString(cur.getColumnIndex("_id"));
            String type = cur.getString(cur.getColumnIndex("type"));
            String numeroTelephone=cur.getString(cur.getColumnIndex("address")).trim();

            if (numeroTelephone.equals(phoneNumberBlocked))
            {
                if (cur.getString(cur.getColumnIndex("type")).equals("6"))
                {                       
                    ContentValues values = new ContentValues();
                    values.put("type", "5");
                    context.getContentResolver().update(uriSMSURI,values,"_id= "+message_id,null);                          
                }
                else if(cur.getString(cur.getColumnIndex("type")).equals("5"))
                {                           context.getContentResolver().delete(uriSMSURI,"_id=?",new String[] { message_id});                      
                }
            }
        }
    }

    @Override
    public boolean deliverSelfNotifications()
    {
        return false;
    }
}

@Override
public void onCreate()
{
    MyContentObserver contentObserver = new MyContentObserver(getApplicationContext());
    ContentResolver contentResolver = getBaseContext().getContentResolver();
    contentResolver.registerContentObserver(Uri.parse(CONTENT_SMS), true, contentObserver);

}

}

<uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />
like image 123
Alaeddine Zidi Avatar answered Nov 15 '22 00:11

Alaeddine Zidi