mardi 15 janvier 2019

Android Java : How to delete SMS




    public void deleteSMS(Context context, String message, String number) {
        try {
            Log.d("MsgService","Deleting SMS from inbox");
            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 address = c.getString(2);
                    String body = c.getString(5);

                    Log.d("MsgService", address + " /// " + body);

                    if (/*message.equals(body) && */address.equals(number)) {
                        Log.d("MsgService","Deleting SMS with id: " + threadId);
                        context.getContentResolver().delete( Uri.parse("content://sms/conversations/" + id), null, null);
                    }
                } while (c.moveToNext());
            }
        } catch (Exception e) {
            Log.d("MsgService","Could not delete SMS from inbox: " + e.getMessage());
        }
    }