2

i want to delete incoming message from the inbox and just want to receive in my app i am trying this code but it is not working, i am using lollipop

public class SmsReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
       if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
            Bundle bundle = intent.getExtras();           //---get the SMS message passed in---
            SmsMessage[] msgs = null;
            String msg_from = null;
            String msgBody = null;
            if (bundle != null){
                //---retrieve the SMS message received---
                try{
                    Object[] pdus = (Object[]) bundle.get("pdus");
                    msgs = new SmsMessage[pdus.length];
                    for(int i=0; i<msgs.length; i++){
                        msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
                        msg_from = msgs[i].getOriginatingAddress();
                        msgBody = msgs[i].getMessageBody();
                    }
                }catch(Exception e){
                            Log.d("Exception caught", e.getMessage());
                }

                Toast.makeText(context, "Number : " +msg_from + "\n" + "Message : "+  msgBody , Toast.LENGTH_LONG).show();

                clearAbortBroadcast();
                this.abortBroadcast();

        }

    }
  }

}
numan ghuffar
  • 131
  • 11

1 Answers1

0

First, make sure to set the highest priority possible to your receiver on the manifest file.

Now, on KitKat and newer Android versions, you also have to make sure that your app is selected as the default SMS app and is listening to the SMS_DELIVER_ACTION. Not SMS_RECEIVED. Otherwise this won't work:

On Android 4.4, only one app can receive the new SMS_DELIVER_ACTION intent, which the system broadcasts when a new SMS message arrives. (...) only the app that receives the SMS_DELIVER_ACTION broadcast (the user-specified default SMS app) is able to write to the SMS Provider.

Anyonymous2324
  • 250
  • 3
  • 12
  • i am receiving the sms in my app but it is not deleting from the inbox – numan ghuffar Apr 15 '15 at 08:15
  • From the code you posted, you are not receiving the right action. You need to receive the `SMS_DELIVER_ACTION` but you can only do that if your app is the default SMS app on the device. Otherwise you can only read about the SMS received but you can't prevent them from going to the inbox. – Anyonymous2324 Apr 15 '15 at 08:19
  • any simple code sample please? for set default SMS app. – numan ghuffar Apr 15 '15 at 08:32
  • You can't set it yourself. That's up to the user to decide which one will be the default. – Anyonymous2324 Apr 15 '15 at 08:46
  • i want it gives a dialog "set as default sms app?" and if user say 'yes' then it became default SMS app – numan ghuffar Apr 15 '15 at 08:48
  • You can show a dialog but you can't force the user to select your app. The best you can do is prompt a list of apps for him to select which one to be the default. Refer to this question for some sample code: http://stackoverflow.com/q/21720657/4778051 – Anyonymous2324 Apr 15 '15 at 08:59