Android LocationProvider启用检查

Android LocationProvider启用检查,android,geolocation,Android,Geolocation,我不知道如何检查LocationProvider是否启用。 在一个应用程序中,我需要显示一些信息,用户应该打开位置服务。 在我关闭了位置的Nexus5测试设备上,仍然可以通过GPS获取真实信息。在系统设置位置,我可以看到位置关闭 final LocationManager manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); try { final

我不知道如何检查LocationProvider是否启用。 在一个应用程序中,我需要显示一些信息,用户应该打开位置服务。 在我关闭了位置的Nexus5测试设备上,仍然可以通过GPS获取真实信息。在系统设置位置,我可以看到位置关闭

    final LocationManager manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    try
    {

        final boolean gpsProviderEnabled = manager.isProviderEnabled(LocationManager.GPS_PROVIDER);
在搜索这个问题时,我也找到了这个解决方案

    String provider = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
在那里,我得到了一个空字符串,在这种情况下,当位置被关闭时,它看起来是正常的


我只是想知道系统上位置是否打开/关闭的最佳方法是什么?

下面的代码检查位置提供程序是否已启用,并发出警报,将用户带到设置屏幕以打开GPS

 LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {

     buildAlertMessageNoGps();

    } else {

     //Location provider is on
    }



private void buildAlertMessageNoGps() {
  final AlertDialog.Builder builder = new AlertDialog.Builder(this);
  builder.setMessage(
    "Your GPS seems to be disabled, do you want to enable it?")
    .setCancelable(false)
    .setPositiveButton("Yes",
      new DialogInterface.OnClickListener() {
       public void onClick(
         @SuppressWarnings("unused") final DialogInterface dialog,
         @SuppressWarnings("unused") final int id) {
        startActivity(new Intent(
          android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));

       }
      })
    .setNegativeButton("No", new DialogInterface.OnClickListener() {
     public void onClick(final DialogInterface dialog,
       @SuppressWarnings("unused") final int id) {
      dialog.cancel();

     }
    });
  final AlertDialog alert = builder.create();
  alert.show();
 }

您可以覆盖onProviderEnabled或onProviderDisabled功能,您可以检查您的gps提供商是否打开

    public class LocationTest extends Activity implements LocationListener {

    LocationManager locationManager = null;
    String best;
    TextView output;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_location_test);
        output = (TextView) findViewById(R.id.output);
        locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        locationManager.requestLocationUpdates(locationManager.GPS_PROVIDER, 0, 0, this);

        log("location provider:");
        dumpProvider();
        Criteria criteria = new Criteria();

        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        best = locationManager.getBestProvider(criteria, false);
        log("Best provider: " + best);
        Location location;
         location= locationManager.getLastKnownLocation("network");
        dumpLocation(location);

        location= locationManager.getLastKnownLocation("gps");
        dumpLocation(location);

        location= locationManager.getLastKnownLocation("passive");
        dumpLocation(location);
    }

    @Override
    public void onLocationChanged(Location location) {
        dumpLocation(location);
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }

    @Override
    public void onProviderEnabled(String provider) {
        log("\nProvider enabled: " + provider);
    }

    @Override
    public void onProviderDisabled(String provider) {
        log("\nProvider disabled: " + provider);
    }

    public void dumpLocation(Location location) {
        if (location == null) {
            log("unknow location");
        } else {
            log(location.toString());
        }

    }

    public void log(String data) {
        output.append(data + "\n");
    }

    public void dumpProvider() {
        List<String> provider = locationManager.getAllProviders();
        for (Iterator<String> i = provider.iterator(); i.hasNext(); ) {
            log((String) i.next());
        }
    }

    @Override
    protected void onResume() {
        super.onResume();

        locationManager.requestLocationUpdates(best, 15000, 1, this);
    }
}

这正是让我困惑的地方,我关闭了GPS和所有其他功能,但在调用manager.isProviderEnabledLocationManager.GPS\u提供程序关闭应用程序并在更改GPS设置后再次打开应用程序时,仍然得到了真实的反馈。有时可能会延迟注意到更新设置。理想情况下,如果GPS关闭,则不应返回真值。我在我们的应用程序中使用了上面的代码,效果很好。你在其他设备上测试过吗?