我使用下面的代码定期检查一段距离后的位置变化。但onLocationChanged从未打过电话。
拥有权限的Android Manifiest文件:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
locationListener = new LocationListener() {
@Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
Toast.makeText(MainActivity1.this,
"Provider enabled: " + provider, Toast.LENGTH_SHORT)
.show();
}
@Override
public void onProviderDisabled(String provider) {
Toast.makeText(MainActivity1.this,
"Provider disabled: " + provider, Toast.LENGTH_SHORT)
.show();
}
@Override
public void onLocationChanged(Location location) {
Toast.makeText(MainActivity1.this, "onLocationChanged",
Toast.LENGTH_SHORT).show();
}
};
locationManager = (LocationManager) this
.getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
0,10, locationListener);
答案 0 :(得分:0)
检查您当前的位置是否为null。 &安培;然后调用onlocationchanged(),如果它在您的活动或服务中为null。
private Location location;
if (location != null) {
mCurrentLoc.onLocationChanged(location);
}
答案 1 :(得分:0)
你只需改变: -
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
0,10, (android.location.LocationListener) this);
那就是它。