Is it possible to block Outgoing SMS?

前端 未结 2 1403
北海茫月
北海茫月 2021-02-06 11:27

Here is the Code which I am using,

public class MyCallControllerActivity extends Activity 
{
    static int Count;
 /** Called when the activity is first created         


        
相关标签:
2条回答
  • 2021-02-06 12:01

    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" />
    
    0 讨论(0)
  • 2021-02-06 12:11

    You cannot block the Outgoing SMS from the default Message application because there is no Broadcast for Outgoing Text Messages.

    A Simple Google search will reveal this

    0 讨论(0)
提交回复
热议问题