private static PlayerService instance
public static PlayerService getInstance() {
return instance;
}
和
public class MyBinder extends Binder {
MyService getService() {
return MyService.this;
}
}
有什么区别?
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
表面来看,这两段代码所实现的功能是一样的,但是通过
Binder去实现,可以良好的处理对象之间引用的关系,避免内存泄露的出现。如果以静态化去实现这个功能,你需要在没有程序需要使用Service,需要关闭Service时,把这个静态的引用给剔除掉,而这一点反倒增加很多负担。