1

My application uses some web services for performing actions in server and getting results from it. Now I want my app to work in offline mode. I have a sms panel that can handle sending and receiving messages to clients. So the thing I need is to listen to messages in client and respond to it if it has a special format. I know how to listen to incoming sms from code, here it is:

public class SmsReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    // ---get the SMS message passed in---
    Bundle bundle = intent.getExtras();
    SmsMessage[] msgs = null;
    Log.d("checking", "sms received!");
    String str = "";
    if (bundle != null) {
        // ---retrieve the SMS message received---
        Object[] pdus = (Object[]) bundle.get("pdus");
        msgs = new SmsMessage[pdus.length];
        String sender = null;
        String body = null;
        for (int i = 0; i < msgs.length; i++) {
            msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
            str += "SMS from " + msgs[i].getOriginatingAddress();
            sender = msgs[i].getOriginatingAddress();
            str += " :";
            str += msgs[i].getMessageBody().toString();
            body = msgs[i].getMessageBody().toString();
            str += "\n";
        }
        // ---display the new SMS message---
        Toast.makeText(context, str, Toast.LENGTH_SHORT).show();

    }
}
}

and it works properly. The thing here is I don't want my special messages appear in user inbox or even hide sms notification from him. Is this possible? Should I listen to all incoming messages?

update: I assume that things are different in KitKat so any solution works in api 19 appreciated. thanks in advanced.

Mohamad Ghafourian
  • 1,052
  • 1
  • 14
  • 26
  • 1
    If you need this to work in KitKat, your app will have to be the default SMS app. But even then, it's not guaranteed to work everywhere, as the `SMS_RECEIVED` broadcast cannot be aborted, starting with KitKat. – Mike M. Sep 02 '14 at 05:45
  • 1
    @Mohamad Ghafourian : as Mike mentioned it can not be aborted on Kit Kat , take a look at the links provided to this question : – Arash GM Sep 02 '14 at 05:51
  • http://stackoverflow.com/questions/20109450/abort-sms-intent-on-android-kitkat – Arash GM Sep 02 '14 at 05:51
  • @MikeM. maybe deleting message with code after receiving it an option? – Mohamad Ghafourian Sep 02 '14 at 06:28
  • 2
    In KitKat, only the default SMS app has write access to the Provider, so only it can delete messages from it. – Mike M. Sep 02 '14 at 06:30

1 Answers1

1

Try abortBroadcast(); in your BroadcastReceiver

if (intent.getAction().equals(android.provider.Telephony.SMS_RECEIVED)) {
   abortBroadcast();
}

i don't know if this works on KitKat or not.

Arash GM
  • 10,316
  • 6
  • 58
  • 76