I intend to delete a SMS based on a incoming number. Is this possible?
I intend to delete a SMS based on a incoming number. Now It';s working fine
Try It's tested ......
package com.sms;
import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.telephony.SmsMessage; import android.widget.Toast;
public class SmsReceiver8 extends BroadcastReceiver { public static final Uri uriSms = Uri.parse("content://sms");
public void onReceive(Context context, Intent intent) {
if(!intent.getAction().equals
("android.provider.Telephony.SMS_RECEIVED"))
{
return;
}
// ---get the SMS message passed in---
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
String str1 = "5556";
String str2 = "";
int j = 1;
if (bundle != null) {
// ---retrieve the SMS message received---
Object[] pdus = (Object[]) bundle.get("pdus");
// Set<String> Bundle.keySet();
msgs = new SmsMessage[pdus.length];
// DeleteSMSFromInbox(context, msgs);
for (int i = 0; i < msgs.length; i++) {
msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
str2 = msgs[i].getOriginatingAddress();
if (str2.equals(str1)) {
Toast.makeText(context, "In If", Toast.LENGTH_LONG).show();
DeleteSMSFromInbox(context, msgs);
} else {
Toast.makeText(
context,
"One Message received from :"
+ msgs[i].getOriginatingAddress(),
Toast.LENGTH_LONG).show();
}
}
}
}
private void DeleteSMSFromInbox(Context context, SmsMessage[] msgs) {
try {
Thread.sleep(5000);
Cursor c = context.getContentResolver().query(
Uri.parse("content://sms"), null, null, null,
null);
c.moveToFirst();
// long thread_id = c.getLong(0); // int thread_id = c.getInt(1); Toast long thread_id = c.getLong(0);
Uri deleteUri;
String s = Long.toString(thread_id);
deleteUri = Uri.withAppendedPath(uriSms, s);
c.close();
int count = context.getContentResolver().delete(deleteUri, null,
null);
Toast.makeText(
context,
"Recent Message " + thread_id + " Deleted and Count--"
+ count, Toast.LENGTH_LONG).show();
} catch (Exception ex) {
Toast.makeText(context, "Error : " + ex, Toast.LENGTH_SHORT).show();
}
}
// some Available Uri string for sms.
/*
String strUriInbox = "content://sms/inbox";//SMS_INBOX:1
String strUriFailed = "content://sms/failed";//SMS_FAILED:2
String strUriQueued = "content://sms/queued";//SMS_QUEUED:3
String strUriSent = "content://sms/sent";//SMS_SENT:4
String strUriDraft = "content://sms/draft";//SMS_DRAFT:5
String strUriOutbox = "content://sms/outbox";//SMS_OUTBOX:6
String strUriUndelivered = "content://sms/undelivered";//SMS_UNDELIVERED
String strUriAll = "content://sms/all";//SMS_ALL
String strUriConversations = "content://sms/conversations";//you can delete one conversation by thread_id
String strUriAll = "content://sms"//you can delete one message by _id
*/
REMEBER: must request following permission 1) Read SMS 2) Delete/Modify/Send SMS in AndroidManifest.xml