通过 wifi/mobile 个网络检测位置
detecting location by wifi/mobile networks
我有这个检测位置的代码:
public class LocationDetector implements LocationListener{
private LocationManager locationManager;
private String provider;
private Location lastloc;
private Context _context;
public LocationDetector(Context context){
_context = context;
}
public Location getlastloc(){
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(provider);
return location;
}
public void start(){
locationManager = (LocationManager) _context.getSystemService(Context.LOCATION_SERVICE);
lastloc = getlastloc();
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 400, 1, (LocationListener) this);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 400, 1, (LocationListener) this);
}
public void stop(){
locationManager.removeUpdates((LocationListener) this);
}
@Override
public void onLocationChanged(Location location) {
Log.d("states", "onLocationChanged()");
lastloc = location;
Log.d("states", "lat: "+lastloc.getLatitude()+"lat: "+lastloc.getLongitude());
}
@Override
public void onProviderDisabled(String arg0) {
}
@Override
public void onProviderEnabled(String arg0) {
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
}
}
问题是 onLocationChanged() 只有在 phone 上启用了 gps 选项并且只有 wifi/mobile 网络选项启用时才会触发 onLocationChanged() 不会触发并且不会检测位置.
即使我已连接到互联网并启用了 wifi/mobile 网络选项,也不会再次检测位置。
所以这个选项是为了什么?它什么时候起作用?
您应该检查需要权限的清单文件。
根据文档,当同时使用 gps 和网络提供商时,您应该定义 access_fine_location,其中包括两者的权限。
查看您的条件,设置空条件时您将不会获得任何更新。
http://developer.android.com/reference/android/location/Criteria.html
我看到您正在使用
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 400, 1, (LocationListener) this);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 400, 1, (LocationListener) this);
首先,您请求从网络提供商处获取更新,然后您再次请求从 GPS 提供商处获取更新。因此,您只能从 GPS 提供商处获得更新。您需要添加以下条件才能满足您的要求。
//if condition to check if GPS is available
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 400, 1, (LocationListener) this);
}
else if (mlocManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 400, 1, (LocationListener) this);
}
我有这个检测位置的代码:
public class LocationDetector implements LocationListener{
private LocationManager locationManager;
private String provider;
private Location lastloc;
private Context _context;
public LocationDetector(Context context){
_context = context;
}
public Location getlastloc(){
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(provider);
return location;
}
public void start(){
locationManager = (LocationManager) _context.getSystemService(Context.LOCATION_SERVICE);
lastloc = getlastloc();
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 400, 1, (LocationListener) this);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 400, 1, (LocationListener) this);
}
public void stop(){
locationManager.removeUpdates((LocationListener) this);
}
@Override
public void onLocationChanged(Location location) {
Log.d("states", "onLocationChanged()");
lastloc = location;
Log.d("states", "lat: "+lastloc.getLatitude()+"lat: "+lastloc.getLongitude());
}
@Override
public void onProviderDisabled(String arg0) {
}
@Override
public void onProviderEnabled(String arg0) {
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
}
}
问题是 onLocationChanged() 只有在 phone 上启用了 gps 选项并且只有 wifi/mobile 网络选项启用时才会触发 onLocationChanged() 不会触发并且不会检测位置.
即使我已连接到互联网并启用了 wifi/mobile 网络选项,也不会再次检测位置。
所以这个选项是为了什么?它什么时候起作用?
您应该检查需要权限的清单文件。 根据文档,当同时使用 gps 和网络提供商时,您应该定义 access_fine_location,其中包括两者的权限。
查看您的条件,设置空条件时您将不会获得任何更新。
http://developer.android.com/reference/android/location/Criteria.html
我看到您正在使用
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 400, 1, (LocationListener) this);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 400, 1, (LocationListener) this);
首先,您请求从网络提供商处获取更新,然后您再次请求从 GPS 提供商处获取更新。因此,您只能从 GPS 提供商处获得更新。您需要添加以下条件才能满足您的要求。
//if condition to check if GPS is available
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 400, 1, (LocationListener) this);
}
else if (mlocManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 400, 1, (LocationListener) this);
}