簡體   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