[英]Location Listener in Background Service Android
Which is the better approach, directly implementing LocationListener
like this 哪种方法更好,直接实现这样的
LocationListener
public class BackgroundService extends Service implements LocationListener {}
or normally declared the LocationListener
inside the class? 或者通常在类中声明
LocationListener
?
LocationListener locationListener = new LocationListener() {};
In the second piece of code you have to call the attribute locationListener
prior to calling the methods of the interface. 在第二段代码中,您必须在调用接口方法之前调用属性
locationListener
。
In the first piece of code you can access the interface methods directly. 在第一段代码中,您可以直接访问接口方法。
So if you know that every method call costs cpu time then to implement it directly in the class rather than putting it as an attribute would be beneficial. 因此,如果您知道每个方法调用花费cpu时间然后直接在类中实现它而不是将其作为属性将是有益的。
In this case you have 1 reference to BackgroundService
with which you can access methods of LocationListener 在这种情况下,您有一个对
BackgroundService
引用,您可以使用它来访问LocationListener的方法
public class BackgroundService extends Service implements LocationListener {}
In this case you have 2 references, one to BackgroundService and another to locationListener 在这种情况下,您有2个引用,一个引用到BackgroundService ,另一个引用到locationListener
public class BackgroundService extends Service {
private LocationListener locationListener = new LocationListener() {};
}
But then again, if your program doesnt have critical time limits, it doesnt really matter. 但话说回来,如果你的程序没有关键时间限制,那真的不重要。 Above all its important that your code is readable.
最重要的是,您的代码是可读的。
I hope that answers your question. 我希望能回答你的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.