【问题标题】:Deleting Android SMS programmatically以编程方式删除 Android SMS
【发布时间】:2012-01-26 16:54:44
【问题描述】:

我想在我的 Android 应用程序中自动删除某些 SMS。因此,我有一种方法可以完全按照我的意愿去做。但是,只有当我将应用程序从 Eclipse 直接部署到我的手机时,它才有效。然后它会删除传入的短信。但是,如果应用程序是从市场上下载的,它就不起作用。但也没有错误。有谁知道我该如何解决这个问题,或者这只适用于有根设备?

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

【问题讨论】:

  • @Marclin 两个问题都不同。在这个问题中,如果 SMS 直接从 Eclipse 部署而不是从 Markrt 位置部署,则将被删除
  • @SunilKumarSahoo:你是对的。该代码完美运行,但前提是设备连接到 PC,而不是从市场上下载,我试图理解为什么?
  • 记得在完成后关闭光标。
  • @Florian - 如您所述,您能否通过添加Handler 来更新您的答案。我会非常感谢你。

标签: android sms


【解决方案1】:

实际上,我帖子中的代码是 100% 正确的。问题是Android在收到短信后需要一些时间来存储短信。所以解决的办法就是加个handler,把delete请求延迟1到2秒。

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

编辑(感谢 Maksim Dmitriev):

请注意,您无法在装有 Android 4.4 的设备上删除 SMS 消息。

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

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

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

How to send SMS messages programmatically

【讨论】:

  • 所以在 android 4.4 中无法发送消息并且不显示在收件箱中??
【解决方案2】:

请注意,您无法在装有 Android 4.4 的设备上删除 SMS 消息。

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

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

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

How to send SMS messages programmatically

【讨论】:

  • 天哪,我正在寻找那几个小时。谢谢!
【解决方案3】:

嘿,使用此代码删除自定义短信 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());
    }

【讨论】:

  • 字符串日期 = c.getString(3); 3 索引是人
  • 自 KitKat 4.4 版本起不允许插入、删除和更新(仅当应用程序是默认 SMS 应用程序或设备已 root 时才允许)。一个可能的解决方法是在这里找到stackoverflow.com/a/27709655/4445814,它只支持 4.4 版本。自 Lollipop 5.1 以来,该安全漏洞已得到修复。
【解决方案4】:

您可以选择哪个应用是 4.4+ 中的默认短信应用,如果您的应用设置为默认应用,它也可以删除短信。

【讨论】:

    【解决方案5】:

    将应用设为默认应用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
            }
    
       }
    

    它会删除所有(收件箱、发件箱、草稿)短信。

    【讨论】:

    • 不会简单地context.getContentResolver().delete(deleteUri, null, null); 也删除所有短信吗?
    • 这个答案显示了如何通过 _id 删除单个短信,如果您知道 _id,那么您不需要查询所有内容,它仅供参考,是的,如果您想删除所有短信,您是对的答案会起作用。
    【解决方案6】:
    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);
    

    【讨论】:

    • 如果您仔细查看我的代码,您会发现这正是我正在做的事情。如果设备通过 ADB 连接到 PC 并且我直接部署它,我的代码可以完美运行。
    【解决方案7】:

    我正在寻找一种一键删除所有短信的方法。感谢这篇文章,我成功了。 如果有人感兴趣,这是我的方法:

        private void deleteSMS(){
        Uri myUri= Uri.parse("content://sms/");
        Cursor cursor = getContext().getContentResolver().query(myUri, null,null,null,null);
        while (cursor.moveToNext()) {
            int thread_id = cursor.getInt(1);
            getContext().getContentResolver().delete(Uri.parse("content://sms/conversations/" + thread_id),null,null);
        }
        cursor.close();
    }
    

    【讨论】:

      【解决方案8】:

      如果您想收到一条消息并且您的短信应用程序您的 Android 设备手机没有发送任何通知,请使用二进制(数据)短信。

      【讨论】:

      • 链接已失效。请检查网址
      猜你喜欢
      • 1970-01-01
      • 2016-11-30
      • 1970-01-01
      • 2011-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-26
      相关资源
      最近更新 更多