是否有一个良好的方式,当短信被Android上读讲? [英] Is there a good way to tell when an SMS gets read on Android?

查看:130
本文介绍了是否有一个良好的方式,当短信被Android上读讲?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在玩弄一个小的通知应用程序。我的基本要求是:当一个短信进来,打开LED。当它被读取,将其关闭。第一部分似乎很容易:我有一个广播接收器在我的清单如下:

I'm playing around with a small notification app. My basic requirements are: when an SMS comes in, turn on the LED. When it gets read, turn it off. The first part seems easy enough: I have a BroadcastReceiver with the following in my manifest:

<intent-filter>
    <action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>

不过,第二部分已被证明更加棘手。目前,我有当消息来自中启动一个服务它运行一个的TimerTask 它运行该code每15秒:

However, the second part has proven more tricky. Currently, I have a service which starts up whenever a message comes in. It runs a TimerTask every 15 seconds which runs this code:

int count = 0;

Uri providerURI = Uri.parse( "content://sms" );
Cursor cursor = this.getContentResolver().query( providerURI, null, "read=0", null, null );

if( cursor != null ) {
    try {
        count = cursor.getCount();
    }
    finally {
        cursor.close();
    }
}

return count;

这似乎工作不够好。当计数为0时,我取消的TimerTask 并停止该服务。不过,我很担心电池寿命。是否有未读消息的变化当数任何形式的一般通知?或这样做的没有更好的办法?

It seems to work well enough. When the count is 0, I cancel the TimerTask and stop the service. However, I'm concerned about battery life. Is there any sort of general notification for when the number of unread messages changes? Or any better way of doing this?

推荐答案

您可以使用ContentObserver。我想,当有新邮件进入短信收件箱,你会得到一个通知(按内容观察者),在另一方面,当短信已被读取,它会从收件箱中被删除,你也将获得一个通知。

You can use ContentObserver. I suppose when a new message comes into sms inbox, you will get a notification (by content observer), on the other hand, when a sms has been read, it will be removed from inbox, and also you will get another notification.

PS,用ContentObserver观察内容://短信/收件箱

ps, Use ContentObserver observing on content://sms/inbox

这篇关于是否有一个良好的方式,当短信被Android上读讲?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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