tags:

views:

17552

answers:

10

Just thought I'd ping this community to see if there's any knowledge on this issue.

On android phones SMS messages registered to applications also get sent to the device's inbox. However to prevent clutter, it'd be nice to be able to remove app specific SMS mesg's from the inbox to reduce the potential overflow of those messages.

Questions on other google groups on getting a definitive answer on a programmatic way to delete SMS messages from the android inbox don't seem to be pressing.

So the scenario:

  • Android App startup.
  • register SMS message types X,Y and Z
  • messages P,Q,X,Y,Z stream in over the course of time, all deposited in inbox
  • Android app detects receipt of X,Y,Z (presumably as part of the program interrupt event)
  • process X,Y,Z
  • Desirement!!! X,Y,Z are deleted from android inbox

Has it been done? Can it be done?

+3  A: 

You'll need to find the URI of the message. But once you do I think you should be able to android.content.ContentResolver.delete(...) it.

Here's some more info.

Neil D
+8  A: 

Thanks nderraugh, that helped. Using those suggestions, I think I got it to work:

(using SDK v1 R2)

It's not perfect, since i need to delete the entire conversation, but for our purposes, it's a sufficient compromise as we will at least know all messages will be looked at and verified. Our flow will probably need to then listen for the message, capture for the message we want, do a query to get the thread_id of the recently inbounded message and do the delete() call.

In our Activity:

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

Note: I wasn't able to do a delete on content://sms/inbox/ or content://sms/all/

Looks like the thread takes precedence, which makes sense, but the error message only emboldened me to be angrier. When trying the delete on sms/inbox/ or sms/all/, you will probably get:

java.lang.IllegalArgumentException: Unknown URL
    at com.android.providers.telephony.SmsProvider.delete(SmsProvider.java:510)
    at android.content.ContentProvider$Transport.delete(ContentProvider.java:149)
    at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:149)

For additional reference too, make sure to put this into your manifest for your intent receiver:

<receiver android:name=".intent.MySmsReceiver">
    <intent-filter>
        <action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
    </intent-filter>
</receiver>

Note the receiver tag does not look like this:

When I had those settings, android gave me some crazy permissions exceptions that didn't allow android.phone to hand off the received SMS to my intent. So, DO NOT put that RECEIVE_SMS permission attribute in your intent! Hopefully someone wiser than me can tell me why that was the case.

dmyung
+5  A: 

So, I had a play, and it is possible to delete a received SMS. Unfortunately it's not all plain sailing :(

I have a receiver that picks up on incoming SMS messages. Now the way the Android SMS incoming routing works is that the piece of code responsible for decoding the messages sends a Broadcast (it uses the sendBroadcast() method - which unfortunately is NOT the version that lets you simply call 'abortBroadcast()') whenever a message arrives.

My receiver may or may not be called before the Systems SMS receiver, and in any case the received broadcast has no property that could reflect the "_id" column in the SMS table.

However, not being one to be stopped that easily I post myself (via a Handler) a delayed message with the SmsMessage as the attached object. (I suppose you could post yourself a Runnable too...)

handler.sendMessageDelayed(handler.obtainMessage(MSG_DELETE_SMS, msg), 2500);

The delay is there to ensure that by the time the message arrives all of the Broadcast receivers will have finished their stuff and the message will be safely ensconced in the SMS table.

When the message (or Runnable) is received here is what I do:

case MSG_DELETE_SMS:
    Uri deleteUri = Uri.parse("content://sms");
    SmsMessage msg = (SmsMessage)message.obj;

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

I use the originating address and timestamp field to ensure a very high probability of deleting ONLY the message I am interested in. If I wanted to be even more paranoid I could include the msg.getMessageBody() content as part of the query.

Yes, the message IS deleted (hooray!). Unfortunately the notification bar is not updated :(

When you open up the notification area you'll see the message sitting there for you... but when you tap on it to open it up - it's gone!

To me, this isn't quite good enough - I want all trace of the message to disappear - I don't want the user to think there is a TXT when there isn't (that would only cause bug reports).

Internally in the OS the phone calls MessagingNotification.updateNewMessageIndicator(Context), but I that class has been hidden from the API, and I did not want to replicate all of that code just for the sake of making the indicator accurate.

Doug
Doug, this is great. As a question, do you know if is it necessary to do the post to the Handler? I wonder if the SMS is inserted into the system SMS database before and of the broadcast receivers are called? I will likely have to look into the OS to see where the insertion occurs, but I would presume that the inserting of the SMS into some database happens before the broadcast receivers are informed. Have any insight into this or did you already check for it?
Hamy
+1  A: 

I think this can not be perfectly done for the time being. There are 2 basic problems:

  1. How can you make sure the sms is already in the inbox when you try to delete it?
    Notice that SMS_RECEIVED is not an ordered broadcast.
    So dmyung's solution is completely trying one's luck; even the delay in Doug's answer is not a guarantee.

  2. The SmsProvider is not thread safe.(refer to http://code.google.com/p/android/issues/detail?id=2916#c0)
    The fact that more than one clients are requesting delete and insert in it at the same time will cause data corruption or even immediate Runtime Exception.

an0
+2  A: 

I couldn't get it to work using dmyung's solution, it gave me an exception when getting either the message id or thread id.

In the end, I've used the following method to get the thread id:

private long getThreadId(Context context) {
 long threadId = 0;

 String SMS_READ_COLUMN = "read";
    String WHERE_CONDITION = SMS_READ_COLUMN + " = 0";
    String SORT_ORDER = "date DESC";
    int count = 0;

    Cursor cursor = context.getContentResolver().query(
                    SMS_INBOX_CONTENT_URI,
          new String[] { "_id", "thread_id", "address", "person", "date", "body" },
                    WHERE_CONDITION,
                    null,
                    SORT_ORDER);

    if (cursor != null) {
            try {
                count = cursor.getCount();
                if (count > 0) {
                    cursor.moveToFirst();
                    threadId = cursor.getLong(1);                              
                }
            } finally {
                    cursor.close();
            }
    }


 return threadId;
}

Then I could delete it. However, as Doug said, the notification is still there, even the message is displayed when opening the notification panel. Only when tapping the message I could actually see that it's empty.

So I guess the only way this would work would be to actually somehow intercept the SMS before it's delivered to the system, before it even reaches the inbox. However, I highly doubt this is doable. Please correct me if I'm wrong.

Stelian Iancu
A: 

i think you can remove the message notification bu calling Cancel method of NotificationManager isn't it

Muhammad Bilal
You will not have the required permission to cancel notifications sent by other applications. You can only cancel your apps notifications.
Noah Seidman
+1  A: 

I'm trying this way but it give me exception require android.permission.READ_SMS.I have added this permission but it again continue with same exception.

Anybody here who can solve this problem for me ?

salman
+1  A: 

Just turn off notifications for the default sms app. Process your own notifications for all text messages!

Noah Seidman
+1  A: 

I'm just getting into android development, and I have a question about this. I'm able to delete SMS from my activity class, but I haven't been able to figure out how I can access those functions from my receiver class. I know it's probablly a simple answer but I've been searching for a while and haven't been able to find an answer. Maybe someone involved in this thread can help? Thanks in advance.

Patrick
+1  A: 

Is there any way to delete a message only (not the conversations)? I can not find a way to do that. who can help me to find the solution? Thank you all!

tulu