From Evernote: |
Managing Network Usage | Android DevelopersClipped from: http://developer.android.com/training/basics/network-ops/managing.html |
Android Doc提到於Activity中操作的方法較為「輕量」,「負擔較小」!
因為定義在Manifest中的話,當Filter攔截到任何broadcast都會喚起你的app,就算你的app並沒有啓動!甚至長達幾個禮拜。
但在Activity中定義的話,若App沒有啓動,那任何Broadcast都不會喚起你的app。
凡事有例外。
若你在Manifest中定義,但你只在某幾個狀況下才需要收到Broadcast,那可以使用setComponentEnabledSetting() 來啟用或關閉 你的 BroadcastReceiver!
Setting up a BroadcastReceiver that gets called unnecessarily can be a drain on system resources. The sample application registers the BroadcastReceiver
NetworkReceiver
in onCreate()
, and it unregisters it in onDestroy()
. This is more lightweight than declaring a <receiver>
in the manifest. When you declare a <receiver>
in the manifest, it can wake up your app at any time, even if you haven't run it for weeks. By registering and unregistering NetworkReceiver
within the main activity, you ensure that the app won't be woken up after the user leaves the app. If you do declare a <receiver>
in the manifest and you know exactly where you need it, you can use setComponentEnabledSetting()
to enable and disable it as appropriate.
沒有留言:
張貼留言