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
When Exactly Are Onsaveinstancestate() and Onrestoreinstancestate() Called
How to Listen for Preference Changes Within a Preferencefragment
How to Draw a Circle with Animation in Android with Circle Size Based on a Value
How to Get Ndk-Gdb Working on Android
Java.Lang.Illegalstateexception: Fragment Not Attached to Activity
Installation Failed with Message Invalid File
How to Create Topic in Fcm Notifications
Download Multiple Files with a Progress Bar in Listview Android
Missing Styles. Is the Correct Theme Chosen for This Layout
Using JSON File in Android App Resources
Android Upload Video to Remote Server Using Http Multipart Form Data
How to Set the Output Image Use Com.Android.Camera.Action.Crop
How to Delete Other Applications Cache from Our Android App
You Cannot Start a Load for a Destroyed Activity in Relativelayout Image Using Glide
Avoiding Content Type Issues When Downloading a File via Browser on Android