How to prevent SMS going to inbox in Android?

前端 未结 5 1005
长发绾君心
长发绾君心 2020-12-23 12:58

I\'m developing a business SMS application. In this app, if an incoming message is from a particular number, say 999999999, it should go to the application\'s inbox and not

5条回答
  •  醉话见心
    2020-12-23 13:10

    When SMS is received by the Android system, it broadcasts an ordered broadcast Intent with action "android.provider.Telephony.SMS_RECEIVED". All registered receivers, including the system default SMS application, receive this Intent in order of priority that was set in their intent-filter. The order for broadcast receirers with the same priority is unspecified. Any BroadcastReceiver could prevent any other registered broadcast receivers from receiving the broadcast using abortBroadcast().

    So, everything you need is broadcast receiver like this:

    public class SmsFilter extends BroadcastReceiver {
    
        @Override
        public void onReceive(Context context, Intent intent) {
    
            if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
                Bundle extras = intent.getExtras();
                if (extras != null) {
                    Object[] pdus = (Object[])extras.get("pdus");
    
                    if (pdus.length < 1) return; // Invalid SMS. Not sure that it's possible.
    
                    StringBuilder sb = new StringBuilder();
                    String sender = null;
                    for (int i = 0; i < pdus.length; i++) {
                        SmsMessage message = SmsMessage.createFromPdu((byte[]) pdus[i]);
                        if (sender == null) sender = message.getOriginatingAddress();
                        String text = message.getMessageBody();
                        if (text != null) sb.append(text);
                    }
                    if (sender != null && sender.equals("999999999")) {
                        // Process our sms...
                        abortBroadcast();
                    }
                    return;
                }
            }
    
            // ...
        }
    }
    

    Looks like the system default SMS processing application uses priority of 0, so you could try 1 for your application to be before it. Add these lines to your AndroidManifest.xml:

    
        
            
        
    
    

    Don't forget about necessary permissions:

    
    

    By the way, you can find all registered receivers and their priorities using this code:

    Intent smsRecvIntent = new Intent("android.provider.Telephony.SMS_RECEIVED");
    List infos = context.getPackageManager().queryBroadcastReceivers(smsRecvIntent, 0);
    for (ResolveInfo info : infos) {
        System.out.println("Receiver: " + info.activityInfo.name + ", priority=" + info.priority);
    }
    

    Update: As FantasticJamieBurn said below, starting from Android 4.4 the only app that can intercept SMS (and block if it wish) is the default SMS app (selected by user). All other apps can only listen for incoming SMS if default SMS app not blocked it.

    See also SMS Provider in the Android 4.4 APIs.

提交回复
热议问题