gpt4 book ai didi

android - 是否有一种好方法可以判断何时在 Android 上读取短信?

转载 作者:搜寻专家 更新时间:2023-11-01 09:15:54 25 4
gpt4 key购买 nike

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

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

最佳答案

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

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

关于android - 是否有一种好方法可以判断何时在 Android 上读取短信?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4709335/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com