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