How to Delete an Sms in Android Before It Reaches the Inbox

How to delete particular inbox message in Android version 5.0 lollipop or in Kitkat?

After 4.4 you are not allowed to delete any sms messages from inbox unless your app is the "default sms app"

Beginning with Android 4.4, the system settings allow users to select a "default SMS app." Once selected, only the default SMS app is able to write to the SMS Provider and only the default SMS app receives the SMS_DELIVER_ACTION broadcast when the user receives an SMS or the WAP_PUSH_DELIVER_ACTION broadcast when the user receives an MMS. The default SMS app is responsible for writing details to the SMS Provider when it receives or sends a new message.

Other apps that are not selected as the default SMS app can only read
the SMS Provider
...

You can see More info here
just mentioned the important part below:

if your app is designed to behave as the default SMS app, then while your app is not selected as the default, it's important that you understand the limitations placed upon your app and disable features as appropriate. Although the system writes sent SMS messages to the SMS Provider while your app is not the default SMS app, it does not write sent MMS messages and your app is not able to write to the SMS Provider for other operations, such as to mark messages as draft, mark them as read, delete them, etc.

Broadcast Receiver for sms - how to block/delete sms before it is read by default messaging app

See How to delete an SMS from the inbox in Android programmatically? and Can we delete an SMS in Android before it reaches the inbox?. Before deleting it, apply logic to search the message for your KEYWORD and then delete only those specific messages.

How can I delete ALL messages in the SMS inbox in Android?

Try deleting with _id :

Cursor c = getApplicationContext().getContentResolver().query(Uri.parse("content://sms/"), null, null, null,null);
try {
while (c.moveToNext()) {
int id = c.getInt(0);
getApplicationContext().getContentResolver().delete(Uri.parse("content://sms/" + id), null, null);
}

}catch(Exception e){
Log.e(this.toString(),"Error deleting sms",e);
}finally {
c.close();
}

Delete an sms from inbox

You can use following method for deleting SMS from Inbox,

private void deleteMessage()
{
Cursor c = getContentResolver().query(SMS_INBOX, null, null, null, null);
//c.moveToFirst();

while (c.moveToNext())
{
System.out.println("Inside if loop");

try
{
String address = c.getString(2);
String MobileNumber = mainmenu.getParameterData().getMobileNumber().trim();

//Log.i( LOGTAG, MobileNumber + "," + address );

Log.i( LOGTAG, c.getString(2) );


if ( address.trim().equals( MobileNumber ) )
{
String pid = c.getString(1);
String uri = "content://sms/conversations/" + pid;
getContentResolver().delete(Uri.parse(uri), null, null);
stopSelf();
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}


Related Topics



Leave a reply



Submit