2016-03-15 36 views
0

Android的位置如果LocationServices被禁用,有沒有什麼辦法,我可以通過WIFI /網絡獲取當前位置。我知道有一種向用戶顯示更改其位置設置的彈出窗口的方法,但我不想顯示彈出窗口。獲取即使LocationServices關閉

那麼即使位置服務被禁用,是否有任何獲取當前位置的方法?的LocationManager.GPS_PROVIDER

整個代碼,以及如何使用

+0

是的,這是可能的。請檢查以下網址以供參考.http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/ – malli

+0

如果用戶已禁用所有位置跟蹤選項設置,那麼您無法通過任何Google API發現他/她的位置。你可以得到最後一個已知的位置,這裏解釋 - http://developer.android.com/training/location/retrieve-current.html。但請記住,位置可能已經過時,在一定程度上或多或少。 – NitroNbg

回答

0

使用LocationManager.NETWORK_PROVIDER,而不是這裏給出的是: http://developer.android.com/guide/topics/location/strategies.html

// Acquire a reference to the system Location Manager 
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); 

// Define a listener that responds to location updates 
LocationListener locationListener = new LocationListener() { 
    public void onLocationChanged(Location location) { 
     // Called when a new location is found by the network location provider. 
     makeUseOfNewLocation(location); 
    } 

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

    public void onProviderEnabled(String provider) {} 

    public void onProviderDisabled(String provider) {} 
    }; 

// Register the listener with the Location Manager to receive location updates 
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener); 
+0

如果LocationServices被禁用,onLocationChanged回調沒有被調用,此代碼不起作用。但是,如果LocationServices已啓用,它將被調用。 –