在Android中查看AGPS设置

时间:2018-12-10 17:44:45

标签: android

我想检查Android中用户的辅助GPS设置。当我更改“位置准确性”设置时,ContentObserver会告诉我URI为content://settings/global/assisted_gps_enabled。我在android.provider.Settings.Global中没有看到这个常量。如何访问此设置的值?

2 个答案:

答案 0 :(得分:1)

尝试以下代码读取该值:

int value = 0;
ContentResolver cr = getContentResolver();

try {
    value = Settings.Global.getInt(cr,"assisted_gps_enabled");
} catch (Exception e) {
    android.util.Log.e("get agps_enabled",e.toString());
}

results on Android 8.0,它返回1(默认情况下启用)

enter image description here

要写入该值:

Settings.Global.putInt(cr, "assisted_gps_enabled", your-value);

此权限<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"/>是必需的,并且此权限仅授予系统应用

答案 1 :(得分:-1)

当用户将GPS设置设为“省电”时,手机中的A-GPS将打开,并且要在您的应用中使用它,您必须添加权限:

android.permission.ACCESS_COARSE_LOCATION

在清单xml文件中,如果您正在android 6.0(Marshmellow)以下运行应用程序,则必须在运行时询问权限, 看这里: https://developer.android.com/training/permissions/requesting

现在,您将如何通过编程方式了解用户已使用AGPS或GPS,您可以为此使用LocationManager类,并分别为AGPS传递LocationManager.NETWORK_PROVIDER;或为GPS传递LocationManager.GPS_PROVIDER

对于GPS提供程序,在android设置中称为“高精度”设置,并且该权限要求为android.permission.ACCESS_FINE_LOCATION 如何使用它? 参见此处:https://developer.android.com/guide/topics/location/strategies#java

有关更多帮助,请查看: 运行时权限:https://developer.android.com/training/permissions/requesting 用于AGPS https://en.wikipedia.org/wiki/Assisted_GPS

// 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.
      makeUseOfNewLocation(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.NETWORK_PROVIDER, 0, 0, locationListener);