以编程方式删除Android SMS

时间:2011-12-23 09:21:23

标签: android sms

我想在我的Android应用程序中自动删除某些SMS。因此,我有一种方法可以完全按照我的意愿行事。但是,它仅在我将应用程序从Eclipse直接部署到我的手机时才有效。然后它删除传入的短信。但是,如果从市场下载应用程序,则无效。但也没有错误。有没有人知道如何解决这个问题,或者这只能在root设备上运行吗?

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

7 个答案:

答案 0 :(得分:28)

实际上,我帖子中的代码是100%正确的。问题是Android收到短信需要一些时间来存储短信。所以解决方案是只添加一个处理程序并将删除请求延迟1或2秒。

这实际上解决了整个问题。

编辑(感谢Maksim Dmitriev):

请注意,您无法在使用Android 4.4的设备上删除短信。

  

此外,系统现在只允许默认应用程序将消息数据写入提供程序,但其他应用程序可以随时阅读。

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

如果你尝试,不会抛出异常;什么都不会被删除。我刚刚在两个模拟器上测试过它。

How to send SMS messages programmatically

答案 1 :(得分:14)

请考虑您无法删除使用Android 4.4的设备上的短信。

  

此外,系统现在只允许默认应用程序写入消息数据   虽然其他应用程序可以随时阅读。

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

如果你尝试,不会抛出异常;什么都不会被删除。我刚刚在两个模拟器上测试过它。

How to send SMS messages programmatically

答案 2 :(得分:8)

嘿,使用此代码删除自定义短信 1.按日期 2.按号码 3.按身体

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

答案 3 :(得分:4)

您可以选择4.4+以上的默认短信应用,如果您的应用设置为默认应用,则也可以删除短信。

答案 4 :(得分:2)

将应用设为默认应用see this。 在删除之前检查您的应用是否为默认短信应用。 使用电话类提供的URI而不是硬编码。

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
        }

   }

删除所有(收件箱,发件箱,草稿)短信。

答案 5 :(得分:1)

private int deleteMessage(Context context, SmsMessage msg) {
    Uri deleteUri = Uri.parse("content://sms");
    int count = 0;
            @SuppressLint("Recycle") 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;
}

使用此代码.............

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

答案 6 :(得分:0)

如果您想收到消息并且您的短信应用程序,则您的android设备电话未使用二进制(数据)短信发送任何通知。 看到这个链接 http://codetheory.in/android-sms/

相关问题