【问题标题】:Is there a good way to tell when an SMS gets read on Android?有没有一种好方法可以判断短信何时在 Android 上被读取?
【发布时间】:2011-01-17 11:35:11
【问题描述】:

我正在玩一个小通知应用程序。我的基本要求是:当有短信进来时,打开 LED。当它被读取时,将其关闭。第一部分似乎很简单:我的清单中有一个BroadcastReceiver,其中包含以下内容:

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

然而,事实证明第二部分更加棘手。目前,我有一个服务,只要有消息进来就会启动。它每 15 秒运行一次 TimerTask,它运行以下代码:

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 并停止服务。但是,我担心电池寿命。当未读消息的数量发生变化时,是否有任何形式的一般通知?或者有什么更好的方法?

【问题讨论】:

    标签: android sms


    【解决方案1】:

    您可以使用 ContentObserver。我想当一条新消息进入短信收件箱时,您会收到一条通知(通过内容观察者),另一方面,当一条短信已被阅读时,它将从收件箱中删除,您也会收到另一条通知。

    ps,使用 ContentObserver 观察 content://sms/inbox

    【讨论】:

    • 经过一些实验,我不得不在 content://mms-sms/ 上使用 ContentObserver 才能正常工作,但它似乎工作正常。谢谢!
    猜你喜欢
    • 2013-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-16
    • 1970-01-01
    • 1970-01-01
    • 2020-08-15
    相关资源
    最近更新 更多