簡單的把常用的地方枚舉一下
功能 | Application | Service | Activity |
---|---|---|---|
Show Dialog | No | No | Yes |
Start Activity | Yes(New Task) | Yes(New Task) | Yes |
Start Service | Yes | Yes | Yes |
Bind Service | Yes | Yes | Yes |
Register BroadcastReceiver | Yes | Yes | Yes |
Send Broadcast | Yes | Yes | Yes |
Layout Inflate | Yes | Yes | Yes |
Load Resource Values | Yes | Yes | Yes |
一般來說能用Application的Context就盡量使用,因為它的生命周期與整個程序同在。而匿名內部類中使用Activity的Context容易造成內存泄露。