简体   繁体   中英

Android: Receiving SMS from specific phone number

Is it possible for my application to receive an SMS from a specific phone number, without letting it trig a system notification, but let all other messages pass to the default SMS application to be treated normally?

If so, how can the system know which process is first on queue to pick which messages to receive?

I didn't try this, but in theory it should work:

In Android SMS broadcast is sent as ordered broadcast , which means that receivers are handled in order and can cancel the broadcast. See SMSDispatcher.java , line 420.

In order to be called first, a receiver must have a higher priority then others.

<intent-filter android:priority="1000" >
    . . .
</intent-filter>

To cancel a broadcast call broadcastReceiver.setResultCode(RESULT_CANCELED) . That way a SMS broadcast will be cancelled and will not be shown by system SMS app (and SMS notifier).

Update:

Also try using broadcastReceiver.setResultCode(Intents.RESULT_SMS_HANDLED) .

Update 2:

user672601 noted in another answer that this indeed works, but he used abortBroadcast() inside broadcast receiver.

I second farhan its not possible for number of reason. Anybody can do anything with such allowance. Check this out for details http://groups.google.com/group/android-developers/browse_thread/thread/78fecbc156f4a1ea

public class NotifyServiceReceiver extends BroadcastReceiver{
static final String ACTION ="android.provider.Telephony.SMS_RECEIVED";
@Override
public void onReceive(Context arg0, Intent arg1) {
    // TODO Auto-generated method stub
    if(arg1.getAction().equalsIgnoreCase(ACTION))
    {
    Bundle extras = arg1.getExtras();

    String strMessage = "private message";
    if ( extras != null )
    {
        Object[] smsextras = (Object[]) extras.get( "pdus" );

        for ( int i = 0; i < smsextras.length; i++ )
        {
            SmsMessage smsmsg = SmsMessage.createFromPdu((byte[])smsextras[i]);

            String strMsgBody = smsmsg.getMessageBody().toString();
            String strMsgSrc = smsmsg.getOriginatingAddress();

            //Toast.makeText(GasService.this,strMessage, Toast.LENGTH_SHORT).show();
            if(strMsgSrc.equals("+919XXXXXXXXX"))
            {
             strMessage += "SMS from " + strMsgSrc + " : " + strMsgBody;               
            Toast.makeText(PrivatesmsService.this,strMessage, Toast.LENGTH_SHORT).show();

            abortBroadcast();
            }

        }

    }
    }

}
 }

you can tell to the system by setting the priority of the activity to 100 or greater then that in manifest file so that when ever you receive sms then your application will access it and by calling abortBroadcast() it will prevent sms reaching to inbox or any other application which has set BroadcastReceiver to receive sms

Peter Knego's answer is correct. I was trying to do this exact thing, tried his solution, and it indeed works except for I used:

this.abortBroadcast();

inside the broadcastReceiver.

I dont think its possible.... because android gives us broadcast Listener which only listen the event. so you have to read every message and check the number if its yours, do an operation else just ignore it.... the default messaging application will automatically handle it....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM