我使用的是精确的代码形式developer.android.com/,但它似乎不能100%工作,我昨天的代码是相同的,并且有效。我拥有所需的所有权限。
<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 :(得分:1)
Android有两个位置提供商--GPS和Coarse(网络)位置。 您需要单独启用它们才能使用每个。 看看这里:
您可以使用以下命令检查是否每个都启用了:
locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);