Android 在尝试使用GPS之前,如何检查GPS是否已启用
我有下面的代码,它不好,因为有时GPS需要很长时间 我怎样才能做到以下几点:Android 在尝试使用GPS之前,如何检查GPS是否已启用,android,gps,Android,Gps,我有下面的代码,它不好,因为有时GPS需要很长时间 我怎样才能做到以下几点: 检查是否启用了GPS 如果启用了GPS,请使用GPS,否则请使用网络提供商 如果GPS需要30秒以上,请使用网络 我可以用我自己的逻辑,使用时间或线程来实现这一点 // Acquire a reference to the system Location Manager LocationManager locationManager = (LocationManager) this.getSys
// 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.
locationCallback(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.GPS_PROVIDER, 0, 0, locationListener);
仅使用此功能,您就可以检查
GPS
可用性
LocationManager mlocManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);;
boolean enabled = mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
没有标准方法,您必须在以下帮助下自行完成:
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
//Do what you need if enabled...
}else{
//Do what you need if not enabled...
}
以及清单中的此权限:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
作为建议,如果GPS未启用,通常标准会指定弹出“位置设置”活动,以便用户可以专门启用它
希望这有帮助
问候 我想你可以用我答案中的代码:
它为您提供了一种非常方便的GPS首次定位和位置更改回调方法。如果GPS需要很长时间才能获得第一个修复,则可以很容易地将GP斯特拉克的实现更改为切换到网络。请再次考虑第三种情况。提高到60秒。30s通常仅适用于使用internet的AGP。此外,要按此答案所示拨打电话,您需要将以下内容添加到AndroidManifest.xml中: