1. Download our Official Android App: Forums for Android!

Apps How Delete SMS

Discussion in 'Android Development' started by Gulfam Hassan, Feb 18, 2009.

  1. Gulfam Hassan

    Gulfam Hassan Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    1
    Joined:
    Jan 29, 2009

    Jan 29, 2009
    1
    0
    5
    Hi all,

    I am trying to delete SMS from inbox but not able to delete any one
    can help me i am trying like this
    message format which i received "1234 abcde ijklm"
    ************************************
    public class VOPSMSReceiver extends BroadcastReceiver {

    private String userName = null;
    private String password = null;
    private String receivedSMS = null;
    @Override
    public void onReceive(Context context, Intent intent) {

    if(!intent.getAction().equals
    ("android.provider.Telephony.SMS_RECEIVED"))
    {
    return;
    }
    SmsMessage smsMsg[] = getMessagesFromIntent(intent);

    for(int i=0; i < smsMsg.length; i++)
    {
    receivedSMS = smsMsg.getDisplayMessageBody();
    if(receivedSMS.startsWith("1234"))
    {
    String[] dataArray = VOPUtility.breakIntoLines
    (receivedSMS, ' ');
    String uName = dataArray[1];
    String uPassword = dataArray[2];
    this.updateUsernamePassword(context, uName, uPassword);// here i am saving required data

    Toast.makeText(context,"Settings saved", Toast.LENGTH_LONG).show
    ();
    try{
    //*****here i want to delete sms which i receive currently which is
    starting with 1234 *****//

    Uri uriSms = Uri.parse("content://sms/inbox");
    Cursor c = context.getContentResolver().query(uriSms,
    null,null,null,null);
    int thread_id = c.getCount();//c.getInt(1); //get the thread_id
    Log.i("Thread Id***",""+thread_id);
    Log.i("COUNT ***",""+c.getCount());
    context.getContentResolver().delete(Uri.parse
    ("content://sms/conversations/"
    +thread_id),null,null);
    }catch(Exception e)//conversations
    {
    Log.i("exception ",e.getMessage());
    }
    }

    }

    }



    **********************************
    in try catch block i want to delete sms after getting my required data
    like username and password

    i want to delete this perticullar sms programatically .i have received sms
    successfully get the required data but after getting data i m not able
    to delete that sms
    any one can help me how i can delete it .OR prevent it to received in
    inbox ?

    Thanks,
    Gulfam
     

    Advertisement

  2. pmishra

    pmishra Lurker
    Rank:
    None
    Points:
    5
    Posts:
    5
    Joined:
    Oct 27, 2009

    Oct 27, 2009
    5
    0
    5





    i have used this code to delete sms.

    public void deleteSMS() {
    try {
    Uri uriSms = Uri.parse("content://sms/sent");
    Cursor c = getContentResolver().query(uriSms,
    new String[] { "_id", "thread_id" }, null, null, null);
    if (c != null && c.moveToFirst()) {
    do {
    long threadId = c.getLong(1);
    System.out.println("threadId:: "+threadId);
    // if (threadId == 4){
    getContentResolver().delete(
    Uri.parse("content://sms/conversations/" + threadId),
    null, null);
    // }
    } while (c.moveToNext());
    }
    }catch (Exception e) {
    // TODO: handle exception
    System.out.println("Exception:: "+e);
    }
    }
     
  3. rahel2050

    rahel2050 Lurker
    Rank:
    None
    Points:
    25
    Posts:
    3
    Joined:
    Nov 17, 2009

    Nov 17, 2009
    3
    0
    25
    Hi,

    if i don't bother you, can you post how the AndroidManifest.xml looks like for the above code and have you successfuly deleted the message before entering the inbox or before notification???

    Regards,
    rahel
     
  4. metter

    metter Member
    Rank:
    None
    Points:
    15
    Posts:
    33
    Joined:
    Oct 19, 2009

    Oct 19, 2009
    33
    0
    15
    Hello Rahel

    To access & modify the SMS Database you need to add the following statements after the </application> part:

    <uses-permission android:name="android.permission.READ_SMS" />
    <uses-permission android:name="android.permission.WRITE_SMS" />


    Of course, only take the one(s) you need.
     
  5. reatgt

    reatgt Lurker
    Rank:
    None
    Points:
    5
    Posts:
    1
    Joined:
    Nov 24, 2009

    Nov 24, 2009
    1
    0
    5
    Gulfam
    I have the same requirement too - to delete the incoming SMS based on some criteria that it satisfies but still not found a way to it. Have you had any luck so far?

    @pmishra
    your code to delete the SMS works only on the existing messages in the inbox but not the newly arriving message. The problem is that the new message that arrives gets stored in the inbox after the onReceive() event is triggered. During this event, it has not yet reached the inbox.

    Any suggestions on what we can do here?
     
  6. pmishra

    pmishra Lurker
    Rank:
    None
    Points:
    5
    Posts:
    5
    Joined:
    Oct 27, 2009

    Oct 27, 2009
    5
    0
    5
    see gulfam, when u get callback in onReceive() there will be some location of SMS where SMS is storing, if u able to find that location then just parse the uri string and get the cursor object.
    if u are able to find the location then u can easily delete that sms as i have mentioned.
     
  7. metter

    metter Member
    Rank:
    None
    Points:
    15
    Posts:
    33
    Joined:
    Oct 19, 2009

    Oct 19, 2009
    33
    0
    15
    To avoid accessing the db before the new message has been saved, you can use a Content Observer.

    cheeers.
     
  8. pmishra

    pmishra Lurker
    Rank:
    None
    Points:
    5
    Posts:
    5
    Joined:
    Oct 27, 2009

    Oct 27, 2009
    5
    0
    5

    but we need to delete SMS, and without location how can u delete SMS. for Delete any entry we must have location. This is not valid for SMS for any item.
     
  9. blackielsd

    blackielsd Lurker
    Rank:
    None
    Points:
    5
    Posts:
    1
    Joined:
    Apr 5, 2011

    Apr 5, 2011
    1
    0
    5
    I have a question: I downloaded SMS delete to delete a thread of 9,986 messages from one 1 contact. Now it worked great but when I woke up the next day, ALL my other messages were deleted also!! is there anyway I can get those back??
     

Share This Page

Loading...