LocationProvider启用检查

时间:2014-05-02 07:54:34

标签: android geolocation

我很困惑如何检查是否启用了LocationProvider。 在APP中,我需要显示某种消息,用户应该打开位置服务。 在我的Nexus 5测试设备上,位置关闭仍然可以在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);

我得到一个空的String返回,在这种情况下看起来没问题,因为位置已关闭

我认为最好的方法是找出系统上是否开启/关闭位置?

2 个答案:

答案 0 :(得分:0)

以下代码检查是否启用了位置提供程序,它还会发出警报,将用户带到设置屏幕以打开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();
 }

答案 1 :(得分:0)

您可以覆盖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);
    }
}