Block incoming/outgoing SMS on Android

后端 未结 2 1392
被撕碎了的回忆
被撕碎了的回忆 2021-02-06 17:52

Does anyone know a reliable way to block incoming/outgoing SMS messages through code? It\'s ok if the actual SMS messages are being received, but I would like to block any notif

2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-06 18:54

    You can't block outgoing text messages.

    Here's what I use for blocking incoming texts.


    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

    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
        
    
    
        
         
          
           
          
         
    
         
          
            
             
            
          
    
    
    

    The main thing to take away from the manifest is the service block, receiver block, and the permissions.

提交回复
热议问题