开发者

Stop watch app using service and BroadcastReceiver

I am working on a program that is kind of like a stop watch, that notifies a user to differen开发者_JS百科t things. They need to be able to use other apps while waiting for the notification. The notification will be text to speech. If the UI screen is open it needs to see how much time is left, and be able to pause the timer.

I am thinking of using a service for the timer and notification. The service will update the UI through intents and BroadcastReceiver. That means I will have to send a broadcast every second for the time.

Is there a better way to do this?


Is there a better way to do this?

A second is a second is a second. Time does not flow faster in one component of your application than another. Hence, you do not need to "send a broadcast every second for the time".

Also, keeping a service in memory that is doing nothing but counting time is a waste of CPU and RAM, the kind of thing that causes users to spew profanity at Android developers while reaching for their favorite task killer.

Use AlarmManager to send an ordered broadcast when the countdown is scheduled to end to handle your Notification (and, optionally, doing something directly in your activity). Also store the countdown end time in a persistent store (database, flat file, etc.), and the activity can use that when it comes back on screen to pick up the countdown. If the user pauses the countdown, cancel the outstanding alarm. If the user sets/re-enables the countdown, set the alarm.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜