2017-07-02 34 views
0

我開發了一個android應用程序,每10秒使用GPS的當前位置,並使用套接字將其發送到服務器。爲了實現這一點,我使用postDelayed方法來獲取當前位置並將其發送到服務器。Android跟蹤應用程序導致設備過熱

myRunnable = new Runnable() { 
    @Override 
    public void run() { 
     Location mCurrentLocation =getCurrentLocation(); 
     if(mCurrentLocation != null) 
      sendCurrentLocationToServer(mCurrentLocation); 
     Handler.postDelayed(this, 10000); 
    }}; 

public Location getCurrentLocation(){ 
    Location currentLocation = myLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
    return currentLocation; 
} 

但是這個代碼使設備過熱,並迅速消耗電池,有另一種方式來達到同樣的效果,降低過熱?

謝謝

回答

0

首先,不要使用getLastKnownLocation。它不僅通常會返回null,循環調用它是一種極其低效的做事方式。相反,requestLocationUpdates,它會在你有一個新的位置時給你打電話。

其次,不要每隔10秒將位置發送到服務器。發送它經常會導致您的手機不斷收聽廣播(無論是wifi還是蜂窩),這會導致發熱問題。發送數據會產生熱量。每分鐘減少一次,併發送標題和速度數據與位置。如果您需要更精確的位置,服務器可以使用速度和標題以編程方式計算可能的位置 - 如果需要的話。很可能你甚至不需要那個。