繁体   English   中英

后台服务Android中的位置监听器

[英]Location Listener in Background Service Android

哪种方法更好,直接实现这样的LocationListener

public class BackgroundService extends Service implements LocationListener {}

或者通常在类中声明LocationListener

LocationListener locationListener = new LocationListener() {};

在第二段代码中,您必须在调用接口方法之前调用属性locationListener

在第一段代码中,您可以直接访问接口方法。

因此,如果您知道每个方法调用花费cpu时间然后直接在类中实现它而不是将其作为属性将是有益的。

在这种情况下,您有一个对BackgroundService引用,您可以使用它来访问LocationListener的方法

public class BackgroundService extends Service implements LocationListener {}

在这种情况下,您有2个引用,一个引用到BackgroundService ,另一个引用到locationListener

public class BackgroundService extends Service {
    private LocationListener locationListener = new LocationListener() {};
}

但话说回来,如果你的程序没有关键时间限制,那真的不重要。 最重要的是,您的代码是可读的。

我希望能回答你的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM