3

I am developing an Android app in which I want to replace message body of last received SMS with some new text.

I am using BroadcastReceiver in which I want to store the message body of last received SMS in a variable and delete the SMS from inbox and Now after deletion I want to put a new encoded message in the inbox.

Now the issue that I am facing is how to delete last received SMS from inbox. I have developed some code in this respect but It delete second last(previous) SMS from inbox. Please check my code below and help that I could continue my app, I would be very thankful to you for this act of kindness.

public void deleteLastSMS()
    {

//      abortBroadcast();

        String body = null;
        String num = null;

        try
        {
            Uri uri = Uri.parse("content://sms/inbox");         
            Cursor c =contex.getContentResolver().query(uri, null, null ,null,null); 
            if(c.moveToFirst()) 
            { 
                body = c.getString(c.getColumnIndexOrThrow("body")).toString();
                num = c.getString(c.getColumnIndexOrThrow("address")).toString();
            }


            int id = c.getInt(0);
            int thread_id = c.getInt(1);
            Uri thread = Uri.parse( "content://sms");
            contex.getContentResolver().delete( thread, "thread_id=? and _id=?", new String[]{String.valueOf(thread_id), String.valueOf(id)} );

        }

        catch(CursorIndexOutOfBoundsException ee)
        {

        }


    }
user2391890
  • 639
  • 1
  • 8
  • 15
  • Are you trying to delete the SMS immediately after receiving it? It might a timing issue - the SMS database might not have been updated yet when you try to delete the last SMS. If this is not the case, you might try other ways of removing the SMS, see http://stackoverflow.com/questions/9389740/delete-an-sms-from-inbox – Juuso Ohtonen Oct 12 '13 at 11:16

1 Answers1

0

I've been looking at this since past hour, this is the stuff i found by far, hop i help :)

void deleteMessage(Context context){
    Uri uriSms = Uri.parse("content://sms/inbox");
    Cursor c = context.getContentResolver().query(uriSms, null,null,null,null);
    int id = c.getInt(0);
    int thread_id = c.getInt(1); //get the thread_id
    context.getContentResolver().delete(Uri.parse("content://sms/conversations/" + thread_id),null,null);
}

void deleteSMS(Context context){
    Uri deleteUri = Uri.parse("content://sms");
    context.getContentResolver().delete(deleteUri, "address=? and date=?", new String[] {strMessageFrom,strTimeStamp});
}

public void deleteSMS1(Context context, String message, String number) {
    try {
        Uri uriSms = Uri.parse("content://sms/inbox");
        Cursor c = context.getContentResolver().query(
                uriSms,
                new String[] { "_id", "thread_id", "address", "person",
                        "date", "body" }, "read=0", null, null);

        if (c != null && c.moveToFirst()) {
            do {
                long id = c.getLong(0);
                long threadId = c.getLong(1);
                String address = c.getString(2);
                String body = c.getString(5);
                String date = c.getString(3);
                Log.e("log>>>",
                        "0>" + c.getString(0) + "1>" + c.getString(1)
                                + "2>" + c.getString(2) + "<-1>"
                                + c.getString(3) + "4>" + c.getString(4)
                                + "5>" + c.getString(5));
                Log.e("log>>>", "date" + c.getString(0));

                if (message.equals(body) && address.equals(number)) {
                    // mLogger.logInfo("Deleting SMS with id: " + threadId);
                    context.getContentResolver().delete(
                            Uri.parse("content://sms/" + id), "date=?",
                            new String[] { c.getString(4) });
                    Log.e("log>>>", "Delete success.........");
                }
            } while (c.moveToNext());
        }
    } catch (Exception e) {
        Log.e("log>>>", e.toString());
    }
}