在后台服务的Andr​​oid位置监听器监听器、后台、位置、oid

2023-09-06 02:44:13 作者:我要快乐

这是更好的方法,直接执行 LocationListener的像这样

 公共类BackgroundService扩展服务实现LocationListener的{}
 

或常宣 LocationListener的里面的类?

  LocationListener的LocationListener的=新LocationListener的(){};
 

解决方案

在第二件code你要调用的属性调用 LocationListener的之前,接口的方法

在第一块$ C $的c可以直接访问接口的方法。

所以,如果你知道,每一个方法调用成本的CPU时间,然后直接实现它的类,而不是把它作为一个属性将是有益的。

在这种情况下,你有1引用 BackgroundService ,使用它可以访问LocationListener的方法

 公共类BackgroundService扩展服务实现LocationListener的{}
 

在这种情况下,你有2个参考文献,一个的 BackgroundService 的,另一个的 LocationListener的的

 公共类BackgroundService延伸服务{
    私人LocationListener的LocationListener的=新LocationListener的(){};
}
 

但话又说回来,如果你的程序可是没有关键的时间限制,它没有真正的问题。 首先它的重要,你的code是可读的。

我希望这能回答你的问题。

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() {};

解决方案

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.