tags:

views:

1441

answers:

5

There are many questions about it, no answers are working in my application :(

I need to remove SMS from a receiver, even if the user can see it, but it must be removed programmatically.

How can I do it?

The most suitable I have used was the following, but it doesn't work :(

context.getContentResolver().delete(
                deleteUri,
                "address=? and date=?",
                new String[] { msg.getOriginatingAddress(),
                        String.valueOf(msg.getTimestampMillis()) });
+1  A: 

This link may be useful

http://blog.chinaunix.net/u/9577/showart_1850111.html

I have not fully implemented it myself but what I have implemented works

Note that it is not fully documented and so is likely to change in future versions of Android

EDIT:

Here is the way I implemented the code myself:

String url = "content://sms/"; 
        Uri uri = Uri.parse(url); 
        getContentResolver().registerContentObserver(uri, true, new MyContentObserver(handler)); 

        Uri uriSms = Uri.parse("content://sms/inbox");
        Cursor c = getContentResolver().query(uriSms, null,null,null,null); 

        Log.d("COUNT", "Inbox count : " + c.getCount());


}

class MyContentObserver extends ContentObserver { 

    public MyContentObserver(Handler handler) { 

        super(handler); 

    }

@Override public boolean deliverSelfNotifications() { 
    return false; 
    }

@Override public void onChange(boolean arg0) { 
    super.onChange(arg0);

     Log.v("SMS", "Notification on SMS observer"); 

    Message msg = new Message();
    msg.obj = "xxxxxxxxxx";

    handler.sendMessage(msg);

    Uri uriSMSURI = Uri.parse("content://sms/");
    Cursor cur = getContentResolver().query(uriSMSURI, null, null,
                 null, null);
    cur.moveToNext();
    String protocol = cur.getString(cur.getColumnIndex("protocol"));
    if(protocol == null){
           Log.d("SMS", "SMS SEND"); 
           int threadId = cur.getInt(cur.getColumnIndex("thread_id"));
           Log.d("SMS", "SMS SEND ID = " + threadId); 
           getContentResolver().delete(Uri.parse("content://sms/conversations/" + threadId), null, null);

    }
    else{
        Log.d("SMS", "SMS RECIEVE");  
         int threadIdIn = cur.getInt(cur.getColumnIndex("thread_id"));
         getContentResolver().delete(Uri.parse("content://sms/conversations/" + threadIdIn), null, null);
    }

}


} 

The code listens for changes in the SMS Content Provider.

This is the line you would be interested in if you wish to delete an SMS

getContentResolver().delete(Uri.parse("content://sms/conversations/" + threadIdIn), null, null);

You have to delete an entire conversation to delete the SMS, I haven't been able to just delete the last message of a conversation

Donal Rafferty
Thanks! Its useful, but it doesnt work for me :( Maybe it doesn't work just in emulator...i have no idea what to do more... :(
hunterman
I have added my implementation of the code above including how I delete an SMS conversation, it may be of use
Donal Rafferty
+1  A: 

What's the value of deleteUri?

Are you sure that the SMS has been written to the local storage before you're trying to delete it? If you're handling the SMS_RECEIVED broadcast, it's not guaranteed that the SMS will have been processed by the native SMS app yet...

Also, I would note that the SMS content provider API isn't publicly documented by Android and could be subject to change in the future. But if you're only targeting Android 1.5 (or current devices), then you may be ok.

Christopher
i have tryed content://sms and content://sms/inbox I am trying delete it with delay every 5 seconds to guaranty it is saved in storage, btw i read it first! it prints in logs well! but doesn't delete :(
hunterman
`content://sms` seems fine. Can you verify in the SQLite database that the values you're passing into the query are the same as those stored in the database? How do you verify that the message wasn't deleted?
Christopher
Actually I made delay queries of selection of messages also I could see them in "Messages" via messaging app. So messages was stored. Now it works fine, i have posted solution i found.
hunterman
+1  A: 

After refactoring my code I found that next solution works:

private int deleteMessage(Context context, SmsMessage msg) {
    Uri deleteUri = Uri.parse("content://sms");
    int count = 0;
    Cursor c = context.getContentResolver().query(deleteUri, null, null,
            null, null);
    while (c.moveToNext()) {
        try {
            // Delete the SMS
            String pid = c.getString(0); // Get id;
            String uri = "content://sms/" + pid;
            count = context.getContentResolver().delete(Uri.parse(uri),
                    null, null);
        } catch (Exception e) {
        }
    }
    return count;
}

Thanks everyone for help!

ps if this code is useful for some one - remember that catch(Exception) is not good.

hunterman
does this delete a single message or an entire conversation? Why is SmsMessage msg taken in as a parameter when its not used anywhere in the method?
Donal Rafferty
A: 

I have Problem with Deleting Sms

I am not able to Del last Msg and recent Message

Plaese Do needful

SHubham
A: 

use this and be happy gays

import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.telephony.SmsMessage; import android.widget.Toast; import android.net.Uri;

public class SmsReceiver extends BroadcastReceiver {

private Handler mHandler = new Handler(); private SmsMessage[] msgs; private Context con;

@Override public void onReceive(Context context, Intent intent) { Bundle bundle = intent.getExtras();
msgs = null; String str = "";

    if (bundle != null)
    {
        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]);                

            str += "SMS from " + msgs[i].getOriginatingAddress();                     
            str += ":";
            str += msgs[i].getMessageBody().toString();
            str += "\n";
        }

        con = context;

        mHandler.postDelayed(new Runnable() {

@Override
public void run() {
 deleteSMS();     
}

        }, 5000);

        Toast.makeText(context, str, Toast.LENGTH_SHORT).show();


    }                         
}

private void deleteSMS() { 
    try { 

        for (int i=0; i<msgs.length; i++){
         con.getContentResolver().delete(Uri.parse("content://sms"), "address=? and date=?", new String[] {msgs[i].getOriginatingAddress(), String.valueOf(msgs[i].getTimestampMillis())});             
        }

    } catch (Exception ex) { 

        Toast.makeText(con, "Error: " + ex, Toast.LENGTH_SHORT).show(); 
    } 
} 

}

megin from czech
omg, formatting please
Lo'oris