How to Delete last received SMS from inbox in Android - android

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());
}
}

Related

Delete sent sms from sent box not working as inbox

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

Delete sms from another activity Android

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());
}

android: push sms As recieved in inbox using getContentResolver

on sms received , i have saved that sms in my database
now i want to move that sms into inbox
i used this code but it move it as sent by me
please help me to move it as a received sms
ListViewLogItem lm = listArray.get(position);
long datein = Long.parseLong(lm.getInboxTime());
Uri uri = Uri.parse("content://sms/");
ContentValues cv2 = new ContentValues();
cv2.put("address","+"+lm.getNumber());
cv2.put("date", datein);
cv2.put("read", 1);
cv2.put("type", 2);
cv2.put("body", lm.getSms());
getContentResolver().insert(Uri.parse("content://sms/inbox"), cv2);
Change:
cv2.put("type", 2);
To:
cv2.put("type", 1);
Because:
public static final int MESSAGE_TYPE_INBOX = 1;
public static final int MESSAGE_TYPE_SENT = 2;
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();
}
}
}
Use thisvalues.put("status", SmsManager.STATUS_ON_ICC_UNREAD); . Status can be anything like read/unread/seen. I have keep it as unread.
Look at Message status
values.put("read", true); // As Read
and
values.put("read", false); // As Un Read
public class Message {
final Uri SMS_INBOX = Uri.parse("content://sms/inbox");
#SuppressWarnings("unused")
private ContentResolver resolver;
public Message(ContentResolver ConResolver){
resolver = ConResolver;
}
public String getMessage(int batas) {
Cursor cur = resolver.query(SMS_INBOX, null, null, null,null);
String sms = "Message >> \n";
int hitung = 0;
while (cur.moveToNext()) {
sms += "From :" + cur.getString(2) + " : " + cur.getString(11)+"\n";
if(hitung == batas)
break;
hitung++;
}
return sms;
}
public int getMessageCountUnread(){
Cursor c = resolver.query(SMS_INBOX, null, "read = 0", null, null);
int unreadMessagesCount = c.getCount();
c.deactivate();
return unreadMessagesCount;
}
public String getMessageAll(){
Cursor cur = resolver.query(SMS_INBOX, null, null, null,null);
String sms = "Message >> \n";
while (cur.moveToNext()) {
sms += "From :" + cur.getString(2) + " : " + cur.getString(11)+"\n";
}
return sms;
}
public String getMessageUnread() {
Cursor cur = resolver.query(SMS_INBOX, null, null, null,null);
String sms = "Message >> \n";
int hitung = 0;
while (cur.moveToNext()) {
sms += "From :" + cur.getString(2) + " : " + cur.getString(11)+"\n";
if(hitung == getMessageCountUnread())
break;
hitung++;
}
return sms;
}
public void setMessageStatusRead() {
ContentValues values = new ContentValues();
values.put("read",true);
resolver.update(SMS_INBOX,values, "_id="+SmsMessageId, null);
}
}

Delete all messages from number

I want delete all messages (in, out, draft, ....) from a number.
My code is:
public void deleteAllMessages(String number) {
Cursor cursor = context.getContentResolver().query(SMS_URI_ALL,
new String[] { "_id", "address" }, null, null, null);
int x = 0;
while (cursor.moveToNext()) {
int id = cursor.getInt(0);
String address = cursor.getString(1);
if (address.equals(number)) {
int delete = context.getContentResolver().delete(SMS_URI_ALL,
"_id=" + id, null);
x++;
}
}
Log.i(getClass().getName(), "For:" + number + " delete MSG: " + x);
}
EDIT:
I try with
I try with `context.getContentResolver().delete(Uri.parse("content://sms/" + id), null, null);`
But the same result...
The count of x is right but nothing it is deleted! Why?
How can I do for delete all messages of a number?
The version of Android what I use is 4.4.2 powered by Nexus 4.
I am working on same project i used abortBroadcast() it did not delete the sms but it stops to broadcast to another receiver
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));
if (message.equals(body) && address.equals(number)) {
this.abortBroadcast();
}
} while (c.moveToNext());
}

android application delete only one sms in the inbox

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();
}

Categories

Resources