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);
Related
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
I access all sms using ("content://sms/inbox") in my custom list view currently i am getting address body and _id now i want to delete selected sms from another activity please guide me i am beginner in andorid
this is my Mainactivity but i want to delete seleted sms from another activity
Uri uri = Uri.parse("content://sms/");
ContentResolver contentResolver = getContentResolver();
Cursor cursor = contentResolver.query(uri, null, null, null, null, null);
if(cursor !=null && cursor.moveToFirst()){
do{
// name = getContactName(address);
tid= cursor.getString(cursor.getColumnIndexOrThrow("_id"));
address = cursor.getString(cursor.getColumnIndexOrThrow("address"));
body = cursor.getString(cursor.getColumnIndexOrThrow("body"));
if(name==null) {
list.add(new mybean("" + address, "" + body,""+tid));
}
else{
list.add(new mybean("" + name, "" + body,""+tid));
}
my =new myadapter(this,list);
lv.setAdapter(my);
}while(cursor.moveToNext());
}
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int pos, long l) {
Intent intent =new Intent(MainActivity.this,Main2Activity.class);
intent.putExtra("delete",list.get(pos).getDel());
intent.putExtra("sms",list.get(pos).getNumber());
intent.putExtra("smsmsg",list.get(pos).getMsg());
startActivity(intent);
}
});
Here is the quide to how to delete sms
Deleting Android SMS programmatically
For kitkat
https://android-developers.googleblog.com/2013/10/getting-your-sms-apps-ready-for-kitkat.html
First you should choose your app as default sms app then you can delete or remove sms from there..
You can also refer to this post
How to delete an SMS from the inbox in Android programmatically?
here is the tutorial for deleting sms programmatically
http://wisdomitsol.com/blog/android/sms/programmatically-delete-sms-in-android
i hope you find these post helpful if any problem you can comment here.
1.First Add permission in manifest
2. write the method
public boolean deleteSms(String smsId) {
boolean isSmsDeleted = false;
try {
mActivity.getContentResolver().delete(
Uri.parse("content://sms/" + smsId), null, null);
isSmsDeleted = true;
} catch (Exception ex) {
isSmsDeleted = false;
}
return isSmsDeleted;
}
you can now delete sms byIds
You can also try 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);
Log.e("log>>>",
"0--->" + c.getString(0) + "1---->" + c.getString(1)
+ "2---->" + c.getString(2) + "3--->"
+ c.getString(3) + "4----->" + c.getString(4)
+ "5---->" + c.getString(5));
Log.e("log>>>", "date" + c.getString(0));
ContentValues values = new ContentValues();
values.put("read", true);
getContentResolver().update(Uri.parse("content://sms/"),
values, "_id=" + id, null);
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());
}
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());
}
}
i want to send SMS from my android device and delete it from mydevice(Sent messages).
SMS are saved in device(4.4.4) but SMS is not deleted with my code. after delete rows affected = 0(Zero).
My device vesrion is 4.4.4.
In other devices, SMS are not being saved. Why does SMS's are saved in Android 4.4.4?
I dont want to save my sent sms's or failure sms's(Which are not sent).
please help me.
My permissions
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />
My Code is to send SMS
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(number, null, msg, null, null);
I am calling the method deleteSMS() from handler with postdelay of 5seconds
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
String message = CastApplication.mPref.getString(context.getResources().getString(R.string.pref_message_to_friend), "");
deleteSMS(context, message, number);
if (MyCastFragment.getInstance() != null) {
MyCastFragment.getInstance().updateView();
}
progressDialog.dismiss();
context.finish();
}
}, 5000);
Delete SMS
public void deleteSMS(Context ctx, String message, String number) {
try {
Uri uriSms = Uri.parse("content://sms");
Cursor c = ctx.getContentResolver().query(uriSms,
new String[] { "_id", "thread_id", "address",
"person", "date", "body" }, null, null, null);
Log.i(TAG, "c count......"+c.getCount());
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 (body.contains(getResources().getText(R.string.invite_text).toString()) && address.equals(number)) {
if (message.equals(body) && address.equals(number)) {
// mLogger.logInfo("Deleting SMS with id: " + threadId);
int rows = ctx.getContentResolver().delete(Uri.parse("content://sms/" + id), "date=?",new String[] { c.getString(4) });
Log.e("log>>>", "Delete success......... rows: "+rows);
Log.e("log>>>", "Delete success......... body: "+body);
}
} while (c.moveToNext());
}
} catch (Exception e) {
Log.e("log>>>", e.toString());
Log.e("log>>>", e.getMessage());
}
}
Unless your app is marked as default SMS app in device, you wont be able to play with SMS Provider, please read SMS guidelines for the same for KITKAT
App can delete Sms at Kitkat WITHOUT default sms app granted.
You need only WRITE_SMS permission
and AppOpps manipulation.
After that your sms can be deleted after 10 seconds after receiving,
notification also will despear.
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();
}