发送或发送SMS的不准确计数,当接收器的数量多于一个 [英] Inaccurate count of sent or outgoing SMS when number of receivers is more than one

查看:163
本文介绍了发送或发送SMS的不准确计数,当接收器的数量多于一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要创建一个运行前台服务,计算用户发送短信息的数量的应用程序。基本上,在服务上,我有一个 ContentObserver 内容://短信哪些过滤器,以<$ C $电话C>的onChange()如果消息类型都有价值 2 ,这意味着它是一个发来的短信。消息类型不属于公共API的一部分,但在这​​里,他们是: http://goo.gl/5XTxe 。这也是最怎么在这里StackOverflow的答案表明,它的完成。

I'm creating an app that runs a foreground service which counts the number of SMSes sent by the user. Basically, in the service, I have a ContentObserver to content://sms which filters calls to onChange() if the message type has value 2, meaning it's a sent SMS. The message types are not part of the public API but here they are: http://goo.gl/5XTxe. This is also how most of the answers here in StackOverflow suggest it's done.

我可以准确地计算发送短信息的数量,当我把它发送到只是一个人,但是当收件人数为两个或两个以上的 ContentObserver 行为非常怪异。出于某种原因,Android可以多次打电话给我的 ContentObserver 的onChange()法类型的消息 2 即使在技术上,它应该只发送一个短信到一个收件人一次。
更糟糕的是,当我打印的logcat的消息而的onChange()被调用,它甚至不显示短信发送到其他日志除了第一个收件人,但它是

I can accurately count the number of sent SMSes when I'm sending it to just one person, but when the number of recipients is two or more, the ContentObserver acts terribly weird. For some reason, Android makes several calls to my ContentObserver's onChange() method with messages of type 2 even if technically, it should only send one SMS to one recipient once. What's worse is, when I print to LogCat the messages for which onChange() was invoked, it doesn't even show logs that the SMS was sent to the other recipients besides the first one, but it was.

我的code的链路是在这里: http://pastie.org/4555034 。这是pretty简单,大约只有150线。

The link to my code is here: http://pastie.org/4555034. It's pretty straightforward, roughly just 150 lines.

我怎样才能准确地保持短信发送计数时,我有多个收件人吗?我怀疑是在管线75我对,而的条件,但我认为Android的行为是更大的罪魁祸首这里。

How can I accurately keep count of the SMS sent when I have more than one recipient? My suspect is my condition for the while at line 75, but I think Android's behavior is the bigger culprit here.

更新(适用于thatsmydoing的问题):计数应该由1 1短信1收件人调升,由2如果有1 SMS 2收件人,3,如果有1短信3收件人,等等(我们假设短信为&lt; = 160个字符现在)。然而,以上我的code,即使有2个,3个或4个收件人时,计数器只加1。我跑它在模拟器上,并在LogCat中想通了,Android的提出了一些2型呼叫的onChange()发送到只有一个收件人,并为其他收件人没有电话,即使测试它在手机上,但它实际上是发送(仍然产生相同日志)。此外,还要求的onChange()为我以前发出的其他消息,不是那个我目前发送。这真是不可思议。

UPDATE (for thatsmydoing's question): The count should be upped by 1 for 1 SMS to 1 recipient, by 2 if there's 1 SMS for 2 recipients, 3 if there's 1 SMS for 3 recipients, and so on (let's assume the SMS is <= 160 characters for now). However, with my code above, even when there are 2, 3, or 4 recipients, the counter is incremented only by 1. I ran it on the emulator and figured out in LogCat that Android makes several type 2 calls to onChange() for sending to only the first recipient, and no calls for the other recipients, even though testing it on a phone, it does in fact send (and still produces the same logs). Moreover, it also calls onChange() for the other messages I have sent before, not the one I'm currently sending. It's really weird.

我有一种感觉,我的code可能不是问题,但我可能会被误解怎么Android的电话的onChange()我的 ContentObserver 内容://短信

I have a feeling that my code might not be the problem, but that I might be misunderstanding how Android calls onChange() on my ContentObserver for content://sms.

推荐答案

我不得不改变我完全走近这个问题的方式。

I had to change the way I approached this problem entirely.

我注意到,而Android表现,同时发送发送SMS未predictable的行为,我可以准确地计算一个当前正在发送,只要它尚未从手机中删除之前发送的短信数量。因此,而不是分配 ContentObserver 内容://短信/ 调用的onChange() 东西时用SMS文件夹时,我简单看了一下内容://短信/ 类型的变化 2 按照设定的计划。

I noticed that while Android exhibits unpredictable behavior while sending an outgoing SMS, I can accurately count the number of text messages sent before the one currently sending, for as long as it hasn't been deleted from the phone. So instead of assigning a ContentObserver to content://sms/ that calls onChange() whenever something occurs with the SMS folder, I simply looked at content://sms/ for changes of type 2 on a set schedule.

唯一的缺点,这是用户甚至可能轮询计划之前删除从发件箱的所有邮件。然而,定义时检查发送的消息一个很好的时间间隔比递增实时计数器一个更为可解的挑战。或者,你可以检查是否有可能当用户试图删除发送消息来触发反击,但截至发稿我看不出如何能在Android中完成。

The only downside to this is that the user might delete all messages from the sentbox even before the polling schedule. However, defining a good time interval for when to check the sent messages is a far more solvable challenge than incrementing the counter in real-time. Or, you can check if it's possible to trigger the counter when the user attempts to delete the sent messages, but as of this writing I can't see how that can be done in Android.

这篇关于发送或发送SMS的不准确计数,当接收器的数量多于一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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