Ali
Ali

Reputation: 22317

Android BroadcastReceiver Vs Service

I am writing a custom analog clock app widget using canvas and ImageView.

I set an alarm for top of every minute in onEnabled event of widget provider. The alarm calls the BroadcastReceiver which is responsible for drawing the clock.

I though of saving the bitmap to some point, so reduce the drawing time. But a BroadcastReceiver dies as soon as the onUpdate returns so I can't keep the bitmap in it.

First I want to know is there anyway to save something in BroadcastReceiver?

I though of using service instead of BroadcastReceiver as they don't get terminated. But somehow I don't know if it's good for this or not?

And I have some general question about services:

  1. I read somewhere that a service may get killed by android system? If it's so, does AlarmManager start it again?

  2. I don't want to wake up the device to update the clock, because it's not important and it can be updated very fast as soon as the device is turned on. I read somewhere that service are run in background. Do using service and calling it with AlarmManager let device to sleep?

And this is how my clock looks:

enter image description here

Upvotes: 1

Views: 504

Answers (1)

Raanan
Raanan

Reputation: 4775

If you want to save something while in broadcastreciever, you can use sharedprefrences or sqllite or a file depending on you needs, and restore it next time. About storage: http://developer.android.com/guide/topics/data/data-storage.html

Broadcastreceivers run at the time they are needed and not take system resources while not needed. Services can be used if you need to wait for random events (like an incoming sms for example), or for long background processing (broadcastrecievers get killed).

Services do get killed by the android system and depending on the service it self they are restored by the system or not. This behaviour is controled by which value the onStartCommand function returns: START_STICKY - restart service with intent = null START_NOT_STICKY - don't restart service

you can read here about the other options: http://developer.android.com/reference/android/app/Service.html#START_STICKY

Upvotes: 1

Related Questions