How to delete the message from onreceive method of broadcast receiver
<p>Am trying to do an application to detect spam message. here I done like taking the incoming messages using broadcast receiver.and am getting the messages too.and am trying to delete the messages when it matches to a particular format.but the problem here is if a spam message came and I can see the notification and its only get deleted only when other spam message came. It's my code. please help me to get the message after I checks with the pattern.thanks in advance.</p>

<pre><code>public class ReceiveMsg extends BroadcastReceiver {

    private String phoneNumber;

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub

        // DONE FIRST
        Bundle bundle = intent.getExtras();
        if(bundle!=null){
        Object[] pdusObj = (Object[]) bundle.get("pdus");
        SmsMessage[] messages = new SmsMessage[pdusObj.length];
     // getting SMS information from Pdu.
        for (int i = 0; i &lt; pdusObj.length; i++) {
            messages[i] = SmsMessage.createFromPdu((byte[]) pdusObj[i]);

        Log.i("Message", messages[i].getMessageBody());
        phoneNumber = messages[i].getOriginatingAddress();

        Toast.makeText(
                context,
                "SMS Received : " + messages[i].getMessageBody() + "From"
                        + messages[i].getOriginatingAddress(), Toast.LENGTH_LONG)
                .show();



        // Checking with black list
        BlackListDao dao = new BlackListDao(context);
        ArrayList&lt;BlackListModel&gt; blackListNos = new ArrayList&lt;BlackListModel&gt;();
        blackListNos = dao.select();

        for (BlackListModel model : blackListNos) {
            Log.d("inside", model.getBlackmobileno());

            if (phoneNumber.equals(model.getBlackmobileno())) {
                Log.i("insideblack", messages[i].getMessageBody());
                goToSpam(messages[i], context);

                return;
            }
        }
        // to check with spamwords

        int value = 0;
        Log.d("inside", "spamwrdcheck");
        value = checkWithSpamWord(messages[i].getMessageBody(), context);
        Log.d("value", "valuespw" + value);
        if (value == 1) {
            goToSpam(messages[i], context);

        } else {
            Log.d("inside", "samplemsgcheck");
            value = checkWithSpamMsg(messages[i].getMessageBody(), context);
            Log.d("value", "valuesmsg" + value);
            if (value == 1) {
                goToSpam(messages[i], context);

            }
        }

    }}
    }

    private int checkWithSpamMsg(String messageBody, Context context) {
        // TODO Auto-generated method stub
        SampleMsgDao dao = new SampleMsgDao(context);
        ArrayList&lt;SampleMsgModel&gt; sampleMsgs = new ArrayList&lt;SampleMsgModel&gt;();
        sampleMsgs = dao.select();

        for (SampleMsgModel model : sampleMsgs) {


            if (messageBody.equals(model.getSampleMsg())) {
                Log.d("value", "return1");
                return 1;
            }

        }
        return 0;
    }

    private void goToSpam(SmsMessage messages, Context context) {
        // TODO Auto-generated method stub
        Log.d("inside", "gotoSpam");

                DateFormat formatter = new SimpleDateFormat(
                        "dd/MM/yyyy hh:mm:ss.SSS");
                Calendar calendar = Calendar.getInstance();
                calendar.setTimeInMillis(messages.getTimestampMillis());

                long now = 1293457709636L;
                calendar.setTimeInMillis(now);

                Log.i("time", "time" + formatter.format(calendar.getTime()));

                String msgToDb = messages.getDisplayMessageBody() + "\n  "
                        + formatter.format(calendar.getTime());

                SpamMsgDao dao = new SpamMsgDao(context);
                int q = dao.insert(messages.getDisplayOriginatingAddress(),
                        msgToDb);
                if (q == 1) {
                    Log.d("spam msg", "inserted");

                } else {
                    Log.d("spam msg", "Not inserted");
                }
                deleteFromInbox(context);



    }

    private int checkWithSpamWord(String msg, Context context) {
        // TODO Auto-generated method stub
        SpamWordDao wordDao = new SpamWordDao(context);
        ArrayList&lt;SpamWordModel&gt; spamWords = new ArrayList&lt;SpamWordModel&gt;();
        spamWords = wordDao.select();
        StringTokenizer token = new StringTokenizer(msg);
        for (SpamWordModel model : spamWords) {
            while (token.hasMoreTokens()) {
                String str = token.nextToken();
                Log.d("message", str);
                Log.d("spamwrd", model.getSpamWord());
                if (str.equals(model.getSpamWord())) {
                    return 1;
                }
            }
        }

        return 0;
    }
    private void deleteFromInbox(Context context) {
        Uri uriSms = Uri.parse("content://sms/inbox");
        Cursor c = context.getContentResolver().query(uriSms, null,
                null, null, null);


        while (c.moveToNext()) {
            Log.d("aaa1", "aaa1");
            int id = c.getInt(0);
            int thread_id = c.getInt(1); 
            int deleted = 0;

            deleted = context.getContentResolver().delete(
                    Uri.parse("content://sms/conversations/"
                            + thread_id),
                    "thread_id=? and _id=?",
                    new String[] { String.valueOf(thread_id),
                            String.valueOf(id) });

            if (deleted != 0) {
                Log.d("messgae", "deleted");
                return;

            } else {
                Log.d("message", "Not deleted");
            }
        }

    }
</code></pre>
