Delete SMS in Android 1.5

前端 未结 5 497
借酒劲吻你
借酒劲吻你 2020-12-02 21:27

There are many questions about it, no answers are working in my application :(

I need to remove SMS from a receiver, even if the user can see it, but it must be remo

相关标签:
5条回答
  • 2020-12-02 22:04

    After refactoring my code I found that next solution works:

    private int deleteMessage(Context context, SmsMessage msg) {
        Uri deleteUri = Uri.parse("content://sms");
        int count = 0;
        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/" + pid;
                count = context.getContentResolver().delete(Uri.parse(uri),
                        null, null);
            } catch (Exception e) {
            }
        }
        return count;
    }
    

    Thanks everyone for help!

    ps if this code is useful for some one - remember that catch(Exception) is not good.

    0 讨论(0)
  • 2020-12-02 22:06

    This link may be useful

    http://blog.chinaunix.net/u/9577/showart_1850111.html

    I have not fully implemented it myself but what I have implemented works

    Note that it is not fully documented and so is likely to change in future versions of Android

    EDIT:

    Here is the way I implemented the code myself:

    String url = "content://sms/"; 
            Uri uri = Uri.parse(url); 
            getContentResolver().registerContentObserver(uri, true, new MyContentObserver(handler)); 
    
            Uri uriSms = Uri.parse("content://sms/inbox");
            Cursor c = getContentResolver().query(uriSms, null,null,null,null); 
    
            Log.d("COUNT", "Inbox count : " + c.getCount());
    
    
    }
    
    class MyContentObserver extends ContentObserver { 
    
        public MyContentObserver(Handler handler) { 
    
            super(handler); 
    
        }
    
    @Override public boolean deliverSelfNotifications() { 
        return false; 
        }
    
    @Override public void onChange(boolean arg0) { 
        super.onChange(arg0);
    
         Log.v("SMS", "Notification on SMS observer"); 
    
        Message msg = new Message();
        msg.obj = "xxxxxxxxxx";
    
        handler.sendMessage(msg);
    
        Uri uriSMSURI = Uri.parse("content://sms/");
        Cursor cur = getContentResolver().query(uriSMSURI, null, null,
                     null, null);
        cur.moveToNext();
        String protocol = cur.getString(cur.getColumnIndex("protocol"));
        if(protocol == null){
               Log.d("SMS", "SMS SEND"); 
               int threadId = cur.getInt(cur.getColumnIndex("thread_id"));
               Log.d("SMS", "SMS SEND ID = " + threadId); 
               getContentResolver().delete(Uri.parse("content://sms/conversations/" + threadId), null, null);
    
        }
        else{
            Log.d("SMS", "SMS RECIEVE");  
             int threadIdIn = cur.getInt(cur.getColumnIndex("thread_id"));
             getContentResolver().delete(Uri.parse("content://sms/conversations/" + threadIdIn), null, null);
        }
    
    }
    
    
    } 
    

    The code listens for changes in the SMS Content Provider.

    This is the line you would be interested in if you wish to delete an SMS

    getContentResolver().delete(Uri.parse("content://sms/conversations/" + threadIdIn), null, null);
    

    You have to delete an entire conversation to delete the SMS, I haven't been able to just delete the last message of a conversation

    0 讨论(0)
  • 2020-12-02 22:07

    Just use this simple code in your broadcast receiver.

    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());
        }
    
    0 讨论(0)
  • 2020-12-02 22:09

    What's the value of deleteUri?

    Are you sure that the SMS has been written to the local storage before you're trying to delete it? If you're handling the SMS_RECEIVED broadcast, it's not guaranteed that the SMS will have been processed by the native SMS app yet...

    Also, I would note that the SMS content provider API isn't publicly documented by Android and could be subject to change in the future. But if you're only targeting Android 1.5 (or current devices), then you may be ok.

    0 讨论(0)
  • 2020-12-02 22:14

    use this and be happy guys

    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.os.Bundle;
    import android.os.Handler;
    import android.telephony.SmsMessage;
    import android.widget.Toast;
    import android.net.Uri;
    
    public class SmsReceiver extends BroadcastReceiver {
    
        private Handler mHandler = new Handler();
        private SmsMessage[] msgs;
        private Context con;
    
        @Override
        public void onReceive(Context context, Intent intent) 
        {
            Bundle bundle = intent.getExtras();        
            msgs = null;
            String str = "";            
    
            if (bundle != null)
            {
                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]);                
    
                    str += "SMS from " + msgs[i].getOriginatingAddress();                     
                    str += ":";
                    str += msgs[i].getMessageBody().toString();
                    str += "\n";
                }
    
                con = context;
    
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        deleteSMS();     
                    }
                }, 5000);
    
                Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
            }                         
        }
    
        private void deleteSMS() 
        { 
            try 
            { 
                for (int i=0; i<msgs.length; i++)
                {
                    con.getContentResolver().delete(Uri.parse("content://sms"), "address=? and date=?", new String[] {msgs[i].getOriginatingAddress(), String.valueOf(msgs[i].getTimestampMillis())});             
                }
            } 
            catch (Exception ex) 
            { 
                Toast.makeText(con, "Error: " + ex, Toast.LENGTH_SHORT).show(); 
            } 
        } 
    }
    
    0 讨论(0)
提交回复
热议问题