删除的Andr​​oid手机短信编程 [英] Deleting Android SMS programatically

查看:101
本文介绍了删除的Andr​​oid手机短信编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的Andr​​oid应用程序会自动删除某些特定的短信。因此,我有这不正是我想要它做的方法。然而,这只是工作,如果我直接将应用程序部署到我的手机从Eclipse中。然后,它会删除收到的短信。然而,它不如果应用程序是从市场上下载的工作。但也有没有错误。有谁知道我可以解决这个问题,或不上植根设备这只工作?

 公共无效deleteSMS(上下文的背景下,字符串消息,串号){
    尝试 {
        mLogger.logInfo(从收件箱中删除短信);
        乌里uriSms = Uri.parse(内容://短信/收件箱);
        光标C = context.getContentResolver()查询(uriSms,
            新的String [] {_id,thread_id单,地址,
                人,日期,身体},NULL,NULL,NULL);

        如果(C =空&安培;!&安培; c.moveToFirst()){
            做 {
                长ID = c.getLong(0);
                长threadId的= c.getLong(1);
                串地址= c.getString(2);
                串体= c.getString(5);

                如果(message.equals(体)及&安培; address.equals(编号)){
                    mLogger.logInfo(删除短信ID:+ threadId的);
                    context.getContentResolver()。删除(
                        Uri.parse(内容://短信/+ ID),NULL,NULL);
                }
            }而(c.moveToNext());
        }
    }赶上(例外五){
        mLogger.logError(无法从收件箱中删除的短信:+ e.getMessage());
    }
}
 

解决方案

其实,code在我的岗位是100%正确的。的问题是,机器人需要一些时间在接收它来存储短信。因此,解决方法就是增加一个处理程序和延缓删除请求1或2秒。

这实际上解决了全部问题。

EDIT(感谢马克西姆·德米特里耶夫):

请考虑您不能删除短信的设备上使用Android 4.4系统。

此外,该系统目前只允许默认的应用程序写入信息数据的提供者,虽然其他应用程序可以在任何时候读取。

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

如果您尝试不将引发异常;什么都不会被删除。我刚才测试了它在两个仿真器。

如何以编程方式发送短信

I want to delete some certain SMS automatically in my Android application. Therefore I have a method which does exactly what I want it to do. However, it only works if I deploy the application directly to my phone from Eclipse. Then it deletes incoming SMS. However, it does not work if the application is downloaded from the market. But there is also no error. Does anybody know how I can solve this or does this only work on rooted devices?

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

解决方案

Actually, the code in my post is 100% correct. The problem was that Android needs some time to store the SMS upon receiving it. So the solution is to just add a handler and delay the delete request for 1 or 2 seconds.

This actually solved the whole issue.

EDIT (thanks to Maksim Dmitriev):

Please consider that you can't delete SMS messages on devices with Android 4.4.

Also, the system now allows only the default app to write message data to the provider, although other apps can read at any time.

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

No exception will be thrown if you try; nothing will be deleted. I have just tested it on two emulators.

How to send SMS messages programmatically

这篇关于删除的Andr​​oid手机短信编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆