2011-08-07 42 views
-2

我打算使用支持GPS的Android手機創建一個不會使用互聯網訪問用戶當前位置的應用程序。如何在不使用互聯網的情況下使用GPS接收器?

我需要知道的是如何在Eclipse中對其進行編碼。你能分享一段Java代碼,它將返回GPS用戶的經度和緯度而不訪問互聯網嗎?

此外,是否有可能使其不能訪問小區網絡 - 直接從衛星獲取用戶的位置?

+0

GPS是純粹的接收器;不涉及雙向溝通。 –

+0

另外,請閱讀:http://developer.android.com/guide/topics/location/obtaining-user-location.html。 –

+0

選中此項: http://stackoverflow.com/questions/33637/how-does-gps-in-a-mobile-phone-work-exactly –

回答

3

以下行添加到您的Android清單,應用標籤上面:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 

然後你需要一個活動或具有這樣的代碼服務:

LocationManager locationManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE); 
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new LocationListener() { 
    onLocationChanged(Location location) { 
     // put code in here to respond to a change in location 
    } 
    onProviderDisabled(String provider) { 
     // put code in here to respond to GPS being disabled 
    } 
    onProviderEnabled(String provider) { 
     // put code in here to respond to GPS being enabled 
    } 
    onStatusChanged(String provider, int status, Bundle extras) { 
     // put code in here to respond to change in GPS status (e.g. GPS becomes unavailable) 
    } 
}); 

編輯:當你呼叫LocationManager locationManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE); locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new LocationListener()... 這設置一個新的LocationListener聽GPS。第一個數字表示每次檢查之間的最小毫秒數(注意,這只是一個指標,系統可能會更頻繁地檢查),第二個數字表示每次檢查之間以米計的最小距離。因此,如果兩者均爲0,則GPS將不斷被檢查,並且一旦可用,新值將被髮送至onLocationChanged()

更多詳細信息,請參閱http://developer.android.com/guide/topics/location/obtaining-user-location.html

+0

tnx爲答案。我應該使用什麼功能從衛星實現實時跟蹤?謝謝 – rahstame

+0

我還在我的回答中增加了som – Pikaling

+0

哪裏是互聯網問題 – Trikaldarshi

相關問題