Search code examples
androidsmsuriandroid-contentprovider

Android - Querying the SMS ContentProvider?


I currently register a content observer on the following URI "content://sms/" to listen out for incoming and outgoing messages being sent.

This seems to work ok and I have also tried deleting from the sms database but I can only delete an entire thread from the following URI "content://sms/conversations/"

Here is the code I use for that

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

}

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); 
           Cursor c = getContentResolver().query(Uri.parse("content://sms/outbox/" + threadId), null, null,
                   null, null);
           c.moveToNext();
           int p = cur.getInt(cur.getColumnIndex("person"));
           Log.d("SMS", "SMS SEND person= " + p); 
           //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);
    }

 }


} 

However I want to be able to get the recipricant and the message text from the SMS Content Provider, can anyone tell me how to do this?

And also how to delete one message instead of an entire thread?


Solution

  • This was already discussed.

    To read SMS from the Content provider check: - android-1-5-reading-sms-messages

    Check this threads:

    About your comment saying that you are deleting a whole thread instead of a single sms: Have you tried out this code?