Remove sms android after receiving no delay

I have code to delete Android programmatic messages programmatically, but when I try to delete it on onReceive, not a single SMS is deleted.

Sample code to remove sms

try { // mLogger.logInfo("Deleting SMS from inbox"); Uri uriSms = Uri.parse("content://sms/inbox"); Cursor c = context.getContentResolver().query( uriSms, new String[] { "_id", "thread_id", "address", "person", "date", "body" }, null, null, null); if (c != null && c.moveToFirst()) { do { long id = c.getLong(0); long threadId = c.getLong(1); String address = c.getString(2); String body = c.getString(5); if (message.equals(body) && address.equals(number)) { // mLogger.logInfo("Deleting SMS with id: " + threadId); context.getContentResolver().delete(Uri.parse("content://sms/" + id), null, null); } } while (c.moveToNext()); } } catch (Exception e) { // mLogger.logError("Could not delete SMS from inbox: " + // e.getMessage()); } 

When I insert this into onReceived , the new SMS is not deleted.

+7
source share
2 answers

You need to add permissions to the manifest file and increase the priority of your class SMS receiver

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

This is due to the fact that he will call SMSReceiver before any operations at the OS level, such as (Saving SMS, notification, Sound SMS, etc.).

Then in SMSReceiver onReceive () you need to add abortBroadcast () to interrupt further broadcasts

 public void onReceive(Context context, Intent intent) { abortBroadcast(); } 

what all

amuses

Ayush Shah

+5
source

You must remove sms with Smsid

 getContentResolver().delete(Uri.parse("content://sms/" + smsid), null, null); 
0
source

All Articles