2012-02-08 113 views
0

我使用GPS數據有一個應用程序,我使用安卓GPS服務的最佳實踐

locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 

是有使用這個服務的最佳實踐。說如果某個應用程序已經調用了這個,我可以使用相同的GPS數據,而不是再次調用服務。

我該怎麼做?

快樂編碼

回答

1

你的問題沒有任何意義的NotificationManager無關與GPS和/或GPS數據。

第二部分回答:您不能重複使用其他應用程序的GPS數據,您只能詢問最後一個已知位置,並希望這不會爲空。如果它爲空,您應該通過使用LocationManager而不是NotificationManager來獲得該位置。

+0

感謝WarrenFaith的答案,我想嘗試一種更有效的方式來使用這個服務,這是一些不會重複調用系統服務的東西。 – rahul 2012-02-09 04:47:11

+0

對不起,我正在使用\t locationManager =(LocationManager)getSystemService(Context.LOCATION_SERVICE); \t \t locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,意外地發佈了NotificationManager,對不起。 – rahul 2012-02-09 04:59:37

0

太從GPS接收數據,你應該使用這樣的代碼:

locationMan= (LocationManager)this.getSystemService(Context.LOCATION_SERVICE); 
locationMan.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener); 
locationMan.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener); 

LocationListener locationListener= new LocationListener() { 
     public void onLocationChanged(Location location) { 

      //TODO be sure the new position is really better then the one already had. 

      lastPos= location; 
     } 

     public void onStatusChanged(String provider, int status, Bundle extras) { 
     } 

     public void onProviderEnabled(String provider) { 
     } 

     public void onProviderDisabled(String provider) { 
     } 
    }; 

這是一個簡單的偵聽器,每一個安卓知道currente位置的改變,它會調用所有偵聽時間,這包括你的應用程序和其他應用程序。因此,無論將多少個監聽器添加到LOCATION_SEVICE,發現位置的過程只運行一次。

一旦你與GPS使用

locationMan.removeUpdates(locationListener); 

進行刪除您的聽衆

-2

確認您已經添加了對網絡接入和GPS許可 確保您已經添加了谷歌地圖API密鑰進入xml