Android 1.5: Reading Sms Messages

Android 1.5: Reading SMS messages

There is a content provider for accessing SMS messages, but it's not documented in the public SDK. If you use ContentResolver.query() with a Uri of content://sms you should be able to access these messages.

You can find more information on this Google Groups thread or previous questions on stackoverflow.

Delete SMS in Android 1.5

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;
}

Reminder: Using catch(Exception) is not recommended.

Android - Querying the SMS ContentProvider?

This was already discussed.

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

Check this threads:

  • delete-sms-in-android-1-5
  • how-to-delete-sms-from-inbox-in-android-programmatically
  • can-we-delete-an-sms-in-android-before-it-reaches-the-inbox

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

How can i access to all the message ( sms ) folder in the phone ?

You can try something like this (to get the destination address and the sms's body):

    private Cursor smsQuerty() {
Uri query_content_uri = Uri.parse("content://sms/sent");

String[] projection = new String[] { "_id", "address", "body" };

Cursor tCursor = getContentResolver().query(query_content_uri,
projection, null, null, null);
startManagingCursor(tCursor);

return tCursor;
}

Than log your cursor to see the data... for example:

private void logCursorForTesting(Cursor cursor) { 
int colCount = cursor.getColumnCount();
Log.d(TAG, "Column count is: " + colCount);

if (cursor != null && cursor.moveToFirst()) {
while(cursor.moveToNext()) {
for (int i = 0; i < colCount; i ++) {
Log.d(TAG, "## >> " + cursor.getString(i));
}
Log.d(TAG, "---------------");
}
}
}

Reading incoming sms

This tutorial is for Android 1.5. Are you even compiling against 1.5 (API Level 3)?

How check if sms/inbox is empty?

Here's the code:

// Retrieve a Cursor pointing to the sms list and the size of it.
Uri uriSMSURI = Uri.parse("content://sms/inbox");
Cursor cur = mContext.getContentResolver().query(uriSMSURI, null, null, null, null);
boolean ret = cur.getCount() > 0;

Remember to close the cursor afterwards.

Android Broadcast Receiver for Sent SMS messages?

Unfortunately there is (currently) no way to implement a BroadcastReceiver because the standard sms application uses a SmsManger to send the messages but specifies concrete internal classes for the sent and delivered intents (SmsReceiver.class and MessageStatusReceiver.class respectively). Not that it is any consolation but you can find the following comment in the Sms application's source:

// TODO: Fix: It should not be necessary to
// specify the class in this intent. Doing that
// unnecessarily limits customizability.

The best alternative seems to be polling content://sms/sent, potentially using a ContentObserver.



Related Topics



Leave a reply



Submit