I made an app that uses the application context to share data and run functions between activities (or when the activity is closed). I also have a BroadcastReceiver
that processes intents, plus it spawns开发者_如何学Go new threads to run common functions stored in the App Context. Since all the activities and the Broadcast receiver can access the App Context by calling this.getApplicationContext()
, it seems to be a much easier way to run stuff in the background while sharing data and functions throughout the app.
But I have a few questions: Is there a downside to doing it this way? Why would I want to create a service if this works flawlessly? (and so far, it has) Is there a larger battery drain doing it this way vs. the service?
I think there is no right answer. If the BroadcastReceiver mechanism is working fine, then go with it. There is no need for a Service. It is better to have a BroadcastReceiver than to have a Service which may be idle for a long time waiting upon some event/action. My 2 cents.
精彩评论