I want to send a SMS and delete the message afterwards (within an hour or two, to be able to check the delivery state).
This is the code I use for sending SMS:
SmsManager.Default.SendTextMessage(contact.Text, null,
message.Text, null, null);
How can I delete the message? I found a code for deleting SMS as below :
ContentResolver.Delete(Android.Net.Uri.Parse("content://sms/" + pid), null, null);
but I don't know how to get pid of sent message.
Try this
void deletSms(string message, string number)
{
Uri uriSms = Uri.Parse("content://sms/sent");
Android.Database.ICursor c = ContentResolver.Query(uriSms, new string[] {"_id", "thread_id","address",
"person", "date", "body" }, null, null, null);
if (c != null)
{
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))
{
Log.Info(tag, "Deleting SMS with id: " + threadId);
ContentResolver.Delete(
Uri.Parse("content://sms/" + id), null, null);
}
} while (c.MoveToNext());
}
}
Related
While upgrading my app into higher version of Android, I'm confused why I cannot delete SMS message on Android version 7.0 to 9.0. It works fine in Android version 6.0.
Why can't I delete all messages in Android version 9.0?
public void deleteSMS(Context context) {
String message ="Notifier,";
try {
Toast.makeText(context, "Deleting SMS from inbox", Toast.LENGTH_SHORT).show();
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 body = c.getString(5);
if (message.equals(body)) {
Toast.makeText(context, "Deleting SMS with id: " + threadId, Toast.LENGTH_SHORT).show();
System.out.println ( threadId );
context.getContentResolver().delete(Uri.parse("content://sms/" + id), null, null);
}
} while (c.moveToNext());
}
} catch (Exception e) {
Toast.makeText(context, "Could not delete SMS from inbox: " + e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
From the documentation, the delete() methods requires three parameters. The first parameter you supplied is wrong. Try this:
contentResolver.delete(Telephony.Sms.CONTENT_URI, "_id=" + Long.toString(i), null);
public void deleteSMS(Context context, String message, String number) {
try {
mLogger.logInfo("Deleting SMS from sent");
Uri uriSms = Uri.parse("content://sms/sent");
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 sent: " + e.getMessage());
}
}
i want to delete sent message but not able to delete sent sms. But sent sms are not deleting so please check the code and tell me where i am doing a mistake
Using this code:
Uri uriSMSURI = Uri.parse("content://sms");
Cursor cur = mContext.getContentResolver().query(uriSMSURI, null, null, null,null);
String phoneNo, msg;
if (cur.moveToFirst()) {
String pid = cur.getString(1);
// do some process
Toast.makeText(mContext, pid, Toast.LENGTH_LONG).show();
Cursor cur2 = mContext.getContentResolver().query(Uri.parse("content://sms/conversations/" + pid), null, null, null,null);
if(cur2.moveToFirst()){
////Tried with this with no luck, its Delete the whole Conversation :
String pid2 = cur2.getString(1);
String uri = "content://sms/conversations/" + pid2;
mContext.getContentResolver().delete(Uri.parse(uri), null, null);
}
}
I am able to Delete the last(whole) Conversation, but I am looking to delete the Last SMS in the last conversation only, not the whole conversation itself, which way to do it?
Problem Solved,
Code :
Uri uriSMSURI = Uri.parse("content://sms/");
Cursor cur = mContext.getContentResolver().query(uriSMSURI, null, null, null, null);
if (cur.moveToFirst()) {
////Changed to 0 to get Message id instead of Thread id :
String MsgId= cur.getString(0);
mContext.getContentResolver().delete(Uri.parse("content://sms/" + MsgId), null, null);
Thanks
I am developing an Android app in which I want to replace message body of last received SMS with some new text.
I am using BroadcastReceiver in which I want to store the message body of last received SMS in a variable and delete the SMS from inbox and Now after deletion I want to put a new encoded message in the inbox.
Now the issue that I am facing is how to delete last received SMS from inbox. I have developed some code in this respect but It delete second last(previous) SMS from inbox. Please check my code below and help that I could continue my app, I would be very thankful to you for this act of kindness.
public void deleteLastSMS()
{
// abortBroadcast();
String body = null;
String num = null;
try
{
Uri uri = Uri.parse("content://sms/inbox");
Cursor c =contex.getContentResolver().query(uri, null, null ,null,null);
if(c.moveToFirst())
{
body = c.getString(c.getColumnIndexOrThrow("body")).toString();
num = c.getString(c.getColumnIndexOrThrow("address")).toString();
}
int id = c.getInt(0);
int thread_id = c.getInt(1);
Uri thread = Uri.parse( "content://sms");
contex.getContentResolver().delete( thread, "thread_id=? and _id=?", new String[]{String.valueOf(thread_id), String.valueOf(id)} );
}
catch(CursorIndexOutOfBoundsException ee)
{
}
}
I've been looking at this since past hour, this is the stuff i found by far, hop i help :)
void deleteMessage(Context context){
Uri uriSms = Uri.parse("content://sms/inbox");
Cursor c = context.getContentResolver().query(uriSms, null,null,null,null);
int id = c.getInt(0);
int thread_id = c.getInt(1); //get the thread_id
context.getContentResolver().delete(Uri.parse("content://sms/conversations/" + thread_id),null,null);
}
void deleteSMS(Context context){
Uri deleteUri = Uri.parse("content://sms");
context.getContentResolver().delete(deleteUri, "address=? and date=?", new String[] {strMessageFrom,strTimeStamp});
}
public void deleteSMS1(Context context, String message, String number) {
try {
Uri uriSms = Uri.parse("content://sms/inbox");
Cursor c = context.getContentResolver().query(
uriSms,
new String[] { "_id", "thread_id", "address", "person",
"date", "body" }, "read=0", 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);
String date = c.getString(3);
Log.e("log>>>",
"0>" + c.getString(0) + "1>" + c.getString(1)
+ "2>" + c.getString(2) + "<-1>"
+ c.getString(3) + "4>" + c.getString(4)
+ "5>" + c.getString(5));
Log.e("log>>>", "date" + c.getString(0));
if (message.equals(body) && address.equals(number)) {
// mLogger.logInfo("Deleting SMS with id: " + threadId);
context.getContentResolver().delete(
Uri.parse("content://sms/" + id), "date=?",
new String[] { c.getString(4) });
Log.e("log>>>", "Delete success.........");
}
} while (c.moveToNext());
}
} catch (Exception e) {
Log.e("log>>>", e.toString());
}
}
hi friends
I am create delete sms
It will delete sms successfully but i want to delete only one sms.
It can possible if possible than how can do it.
If its code available please send me.
Please help me.
Thanks in advance.
Simply use this code.
try {
Uri uriSms = Uri.parse("content://sms/inbox");
Cursor c = context.getContentResolver().query(
uriSms,
new String[] { "_id", "thread_id", "address", "person",
"date", "body" }, "read=0", 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);
String date = c.getString(3);
if (message.equals(body) && address.equals(number)) {
// mLogger.logInfo("Deleting SMS with id: " + threadId);
context.getContentResolver().delete(
Uri.parse("content://sms/" + id), "date=?",
new String[] { <your date>});
Log.e("log>>>", "Delete success.........");
}
} while (c.moveToNext());
}
} catch (Exception e) {
Log.e("log>>>", e.toString());
}
For same thing i am looking for.
I am using this code with count ,Check if it works :
public void deleteOneSMS(int threadIdNo) {
try {
Uri uriSms = Uri.parse("content://sms/inbox");
Cursor c = getContentResolver().query(uriSms,new String[] { "_id", "thread_id" }, null, null, null);
if (c != null && c.move(threadIdNo)) {
do {
long threadId = c.getLong(1);
count++;
//System.out.println("threadId:: "+threadId);
if (count == threadIdNo){
getContentResolver().delete(
Uri.parse("content://sms/conversations/" + threadId),
null, null);
}
} while (c.moveToNext());
}
}catch (Exception e) {
// TODO: handle exception
System.out.println("Exception:: "+e);
}
}
If you want to delete only one sms at a time and not all conversation, then this example will help you, here i am getting the lates(top most) sms from inbox and delete them, remember that each sms has its thread and id value which differentiate it from other sms.
try
{
Uri uri = Uri.parse("content://sms/inbox");
Cursor c =v.getContext().getContentResolver().query(uri, null, null ,null,null);
String body = null;
String number = null;
if(c.moveToFirst())
{}
}
catch(CursorIndexOutOfBoundsException ee)
{
Toast.makeText(v.getContext(), "Error :"+ ee.getMessage() ,Toast.LENGTH_LONG).show();
}