Eliminar Android SMS mediante programación


Quiero eliminar algunos ciertos SMS automáticamente en mi aplicación Android. Por lo tanto, tengo un método que hace exactamente lo que quiero que haga. Sin embargo, solo funciona si despliego la aplicación directamente a mi teléfono desde Eclipse. Luego elimina los SMS entrantes. Sin embargo, no funciona si la aplicación se descarga del mercado. Pero tampoco hay error. ¿Alguien sabe cómo puedo resolver esto o solo funciona en dispositivos rooteados?

public void deleteSMS(Context context, String message, String number) {
    try {
        mLogger.logInfo("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);

                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 inbox: " + e.getMessage());
    }
}
 29
Author: Cœur, 2011-12-23

6 answers

En realidad, el código en mi post es 100% correcto. El problema era que Android necesita algo de tiempo para almacenar el SMS al recibirlo. Por lo tanto, la solución es simplemente agregar un controlador y retrasar la solicitud de eliminación durante 1 o 2 segundos.

Esto realmente resolvió todo el problema.

EDITAR (gracias a Maksim Dmitriev):

Tenga en cuenta que no puede eliminar mensajes SMS en dispositivos con Android 4.4.

Además, el sistema ahora solo permite que la aplicación predeterminada escriba un mensaje datos al proveedor, aunque otras aplicaciones pueden leer en cualquier momento.

Http://developer.android.com/about/versions/kitkat.html

No se lanzará ninguna excepción si lo intenta; nada se eliminará. Acabo de probarlo en dos emuladores.

Cómo enviar mensajes SMS mediante programación

 27
Author: Florian,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-05-29 14:58:23

Tenga en cuenta que no puede eliminar mensajes SMS en dispositivos con Android 4.4.

Además, el sistema ahora solo permite que la aplicación predeterminada escriba datos de mensajes al proveedor, aunque otras aplicaciones pueden leer en cualquier momento.

Http://developer.android.com/about/versions/kitkat.html

No se lanzará ninguna excepción si lo intenta; nada se eliminará. Acabo de probarlo en dos emuladores.

Cómo enviar mensajes SMS programáticamente

 12
Author: Maksim Dmitriev,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-11-15 10:30:26

Hey utilice este código para eliminar personalizar sms 1. Por fecha 2. Por número 3. Por cuerpo

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());
    }
 8
Author: Ashekur Rahman Molla Asik,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-03-31 18:56:36

Puede elegir qué aplicación es la aplicación SMS predeterminada en 4.4+ y si su aplicación está configurada como predeterminada, también podrá eliminar SMS.

 3
Author: Sites-For-Less.com,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-09-29 16:08:37

Para hacer app como app por defecto ver esto. Compruebe si su aplicación es la aplicación sms predeterminada antes de eliminarla. Utilice el URI proporcionado por la clase telephony en lugar de hardcoding.

public void deleteSMS(Context context,int position)
{
    Uri deleteUri = Uri.parse(Telephony.Sms.CONTENT_URI);
    int count = 0;
    Cursor c = context.getContentResolver().query(deleteUri, new String[]{BaseColumns._ID}, null,
            null, null); // only query _ID and not everything
        try {
              while (c.moveToNext()) {
                // Delete the SMS
                String pid = c.getString(Telephony.Sms._ID); // Get _id;
                String uri = Telephony.Sms.CONTENT_URI.buildUpon().appendPath(pid)
                count = context.getContentResolver().delete(uri,
                    null, null);
              }
        } catch (Exception e) {
        }finally{
          if(c!=null) c.close() // don't forget to close the cursor
        }

   }

Elimina todos(bandeja de entrada,bandeja de salida,borrador) los SMS.

 2
Author: Kishor N R,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-09-12 03:26:16
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;
}

use this code.............

O

getContentResolver().delete(Uri.parse("content://sms/conversations/" + threadIdIn), null, null);
 1
Author: Nikhilreddy Gujjula,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-12-23 09:37:58