Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
288 views
in Technique[技术] by (71.8m points)

android - Delete an sms from inbox

I would like to delete an SMS from the inbox once it is read by the user. How to do this?

Edit:

public class SmsReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub

    Bundle bundle = intent.getExtras();        
    SmsMessage[] msgs = null;
    String address = null;

    if(bundle!=null)  {
        String info = " ";
        Object[] pdus = (Object[]) bundle.get("pdus");
        msgs = new SmsMessage[pdus.length];

        for (int i=0; i<msgs.length; i++) {
            msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);                
                address=msgs[i].getDisplayOriginatingAddress();
                info += msgs[i].getMessageBody().toString();

        } 
        /*String str=bundle.getString("state");
        Log.v("State",str);*/

        if((PhoneNumberUtils.isWellFormedSmsAddress(address))){             //set ! and address length

        //abortBroadcast();
        Log.v("phone num","wellformed");
            Uri deleteUri = Uri.parse("content://sms");

            Cursor c = context.getContentResolver().query(deleteUri, null, null,
                    null, null);
            while (c.moveToNext()) {
                try {
                    // Delete the SMS
                    String pid = c.getString(0); // Get id;
                    String uri = "content://sms/conversations/" + pid;
                   context.getContentResolver().delete(Uri.parse(uri),
                            null, null);
                } catch (Exception e) {
                    Log.v("exception","occurred");
                }
            }

        }

    }
}

}

What is wrong with this code? The sms is not getting deleted

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...