2014-09-28 88 views
1

我使用的確切代碼形式developer.android.com/但它似乎不工作100%,我昨天是相同的代碼,它的工作。我擁有所有必需的權限。onLocationChanged從來沒有叫過?

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

我已經添加了ACCESS_COARSE_LOCATION權限,以防萬一先不起作用。

代碼:

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

Log.i("DEV", "LocationListener"); 
// Define a listener that responds to location updates 
LocationListener locationListener = new LocationListener() { 
    public void onLocationChanged(Location location) {Log.i("DEV", "onLocationChanged");} 

    public void onStatusChanged(String provider, int status, Bundle extras) {Log.i("DEV", "onStatusChanged");} 

    public void onProviderEnabled(String provider) {Log.i("DEV", "onProviderEnabled");} 

    public void onProviderDisabled(String provider) {Log.i("DEV", "onProviderDisabled");} 
}; 

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

我已經打開了移動數據或WiFi總是相同日誌輸出

09-28 20:06:56.816 21408-21408/smst.dev.mav.com I/DEV﹕ LocationManager 
09-28 20:06:56.826 21408-21408/smst.dev.mav.com I/DEV﹕ LocationListener 
09-28 20:06:56.826 21408-21408/smst.dev.mav.com I/DEV﹕ Register listener 
09-28 20:06:56.826 21408-21408/smst.dev.mav.com I/DEV﹕ onProviderDisabled 

謝謝!

+0

雖然很晚了,但我希望這個意見可以幫助別人,看看這個鏈接:http://www.lengrand.fr/2013/10/onlocationchanged-is-never-called-on-android – SMHJamali 2017-03-07 20:40:37

回答

2

Android有兩個位置提供商 - GPS和粗略(網絡)位置。 您需要單獨啓用這兩者才能使用每一個。這裏 看看:

enter image description here

您可以檢查是否每個人通過使用啓用: locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);