Location Listener in Background Service Android

给你一囗甜甜゛ 提交于 2019-11-29 14:54:16

问题


Which is the better approach, directly implementing LocationListener like this

public class BackgroundService extends Service implements LocationListener {}

or normally declared the LocationListener inside the class?

LocationListener locationListener = new LocationListener() {};

回答1:


In the second piece of code you have to call the attribute locationListenerprior to calling the methods of the interface.

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.

In this case you have 1 reference to BackgroundService with which you can access methods of LocationListener

public class BackgroundService extends Service implements LocationListener {}

In this case you have 2 references, one to BackgroundService and another to 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.



来源:https://stackoverflow.com/questions/12227446/location-listener-in-background-service-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!