对多个提供商使用 requestLocationUpdates

发布于 2024-10-02 07:51:18 字数 248 浏览 0 评论 0原文

(这是关于 android 中的 LocationManager 类)。

有没有一种方法可以使用 requestLocationUpdates,但以某种方式允许它每次都为我提供最佳活动提供程序的结果?我可以将 getBestProvider 的结果传递给它,但它总是会从该提供程序返回结果,并且如果用户打开/关闭 GPS,它就不会按我的预期工作。

我的代码位于后台服务中。

(this is about the LocationManager class in android).

Is there a way of using requestLocationUpdates, but in some way that allows it to give me results for the best active provider every time? I could pass it the result of getBestProvider, but then it will always return results from that provider, and it wouldn't work as I expect if the user turns the gps on/off.

My code is in a background service.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一杆小烟枪 2024-10-09 07:51:18

是否有一种使用 requestLocationUpdates 的方法,但以某种方式允许它每次都为我提供最佳活动提供程序的结果?

不需要,但我们欢迎您为多个提供商注册单独的 LocationListeners

我可以将 getBestProvider 的结果传递给它,但它总是会返回来自该提供程序的结果,并且如果用户打开/关闭 GPS,它就不会按我的预期工作。

当您的 LocationListener 提供程序启用和禁用时,您会收到通知。如果您当前的提供程序已禁用,您可以向备用提供程序注册 LocationListener

Is there a way of using requestLocationUpdates, but in some way that allows it to give me results for the best active provider every time?

No, though you are welcome to register separate LocationListeners for multiple providers.

I could pass it the result of getBestProvider, but then it will always return results from that provider, and it wouldn't work as I expect if the user turns the gps on/off.

Your LocationListener is notified when its provider is enabled and disabled. If your current provider is disabled, you might register a LocationListener with a backup provider.

傲影 2024-10-09 07:51:18

这就是我所做的:

public class LocationActivity extends Activity implements LocationListener{

    private TextView latituteField;
    private TextView longitudeField;
    private LocationManager locationManager;
    private String provider;
    private TextView outputField;
    private Location location;
    private ScrollView scrollView;
    private Criteria criteria;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_location);

        latituteField = (TextView) findViewById(R.id.lat_textView);
        longitudeField = (TextView) findViewById(R.id.long_textView);
        outputField = (TextView) findViewById(R.id.output_textView);
        scrollView = (ScrollView) findViewById(R.id.scrollView1);

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

        criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);

        List<String> providers = locationManager.getProviders(criteria, true);
        outputField.append("Providers available..." + "\n");
        for (String provider : providers) {
            outputField.append(provider + "\n");
            scrollView.fullScroll(ScrollView.FOCUS_DOWN);
        }

        provider = locationManager.getBestProvider(criteria, true);
        locationManager.requestLocationUpdates(provider, 400, 1, this);

        if (provider != null) {
            outputField.append("Provider " + provider + " has been selected." + "\n");
            scrollView.fullScroll(ScrollView.FOCUS_DOWN);

            if (location != null) {
                onLocationChanged(location);
            } else {
                latituteField.setText("Location not available");
                longitudeField.setText("Location not available");
            }
          } else {
            outputField.append("No provider selected" + "\n");
            scrollView.fullScroll(ScrollView.FOCUS_DOWN);
          }
    }

    @Override
      protected void onResume() {
        super.onResume();
        locationManager.requestLocationUpdates(provider, 400, 1, this);
      }

    protected void onPause() {
        super.onPause();
        locationManager.removeUpdates(this);
      }

    @Override
    public void onLocationChanged(Location location) {
        double lat =location.getLatitude();
        double lng =location.getLongitude();
        latituteField.setText(String.valueOf(lat));
        longitudeField.setText(String.valueOf(lng));
        outputField.append("New Location: " + String.valueOf(lat) + " " + String.valueOf(lng) + "\n");
        scrollView.fullScroll(ScrollView.FOCUS_DOWN);
    }

    @Override
    public void onProviderDisabled(String dProvider) {
        outputField.append("Provider " + dProvider + " has been disabled." + "\n");
        scrollView.fullScroll(ScrollView.FOCUS_DOWN);

        provider = locationManager.getBestProvider(criteria, true);
        locationManager.requestLocationUpdates(provider, 400, 1, this);

        if (provider != null) {
            outputField.append("Provider " + provider + " has been selected." + "\n");
            scrollView.fullScroll(ScrollView.FOCUS_DOWN);

            if (location != null) {
                onLocationChanged(location);
            } else {
                latituteField.setText("Location not available");
                longitudeField.setText("Location not available");
            }
          } else {
            outputField.append("No provider selected" + "\n");
            scrollView.fullScroll(ScrollView.FOCUS_DOWN);
          }
    }

    @Override
    public void onProviderEnabled(String eProvider) {
        outputField.append("Provider " + eProvider + " has been enabled." + "\n");
        scrollView.fullScroll(ScrollView.FOCUS_DOWN);

        provider = locationManager.getBestProvider(criteria, true);
        locationManager.requestLocationUpdates(provider, 400, 1, this);

        if (provider != null) {
            outputField.append("Provider " + provider + " has been selected." + "\n");
            scrollView.fullScroll(ScrollView.FOCUS_DOWN);

            if (location != null) {
                onLocationChanged(location);
            } else {
                latituteField.setText("Location not available");
                longitudeField.setText("Location not available");
            }
          } else {
            outputField.append("No provider selected" + "\n");
            scrollView.fullScroll(ScrollView.FOCUS_DOWN);
          }
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        outputField.append("Provider " + provider + " status changed to: " + Integer.toString(status) + "\n");
        scrollView.fullScroll(ScrollView.FOCUS_DOWN);
    }
}

This is what I do:

public class LocationActivity extends Activity implements LocationListener{

    private TextView latituteField;
    private TextView longitudeField;
    private LocationManager locationManager;
    private String provider;
    private TextView outputField;
    private Location location;
    private ScrollView scrollView;
    private Criteria criteria;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_location);

        latituteField = (TextView) findViewById(R.id.lat_textView);
        longitudeField = (TextView) findViewById(R.id.long_textView);
        outputField = (TextView) findViewById(R.id.output_textView);
        scrollView = (ScrollView) findViewById(R.id.scrollView1);

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

        criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);

        List<String> providers = locationManager.getProviders(criteria, true);
        outputField.append("Providers available..." + "\n");
        for (String provider : providers) {
            outputField.append(provider + "\n");
            scrollView.fullScroll(ScrollView.FOCUS_DOWN);
        }

        provider = locationManager.getBestProvider(criteria, true);
        locationManager.requestLocationUpdates(provider, 400, 1, this);

        if (provider != null) {
            outputField.append("Provider " + provider + " has been selected." + "\n");
            scrollView.fullScroll(ScrollView.FOCUS_DOWN);

            if (location != null) {
                onLocationChanged(location);
            } else {
                latituteField.setText("Location not available");
                longitudeField.setText("Location not available");
            }
          } else {
            outputField.append("No provider selected" + "\n");
            scrollView.fullScroll(ScrollView.FOCUS_DOWN);
          }
    }

    @Override
      protected void onResume() {
        super.onResume();
        locationManager.requestLocationUpdates(provider, 400, 1, this);
      }

    protected void onPause() {
        super.onPause();
        locationManager.removeUpdates(this);
      }

    @Override
    public void onLocationChanged(Location location) {
        double lat =location.getLatitude();
        double lng =location.getLongitude();
        latituteField.setText(String.valueOf(lat));
        longitudeField.setText(String.valueOf(lng));
        outputField.append("New Location: " + String.valueOf(lat) + " " + String.valueOf(lng) + "\n");
        scrollView.fullScroll(ScrollView.FOCUS_DOWN);
    }

    @Override
    public void onProviderDisabled(String dProvider) {
        outputField.append("Provider " + dProvider + " has been disabled." + "\n");
        scrollView.fullScroll(ScrollView.FOCUS_DOWN);

        provider = locationManager.getBestProvider(criteria, true);
        locationManager.requestLocationUpdates(provider, 400, 1, this);

        if (provider != null) {
            outputField.append("Provider " + provider + " has been selected." + "\n");
            scrollView.fullScroll(ScrollView.FOCUS_DOWN);

            if (location != null) {
                onLocationChanged(location);
            } else {
                latituteField.setText("Location not available");
                longitudeField.setText("Location not available");
            }
          } else {
            outputField.append("No provider selected" + "\n");
            scrollView.fullScroll(ScrollView.FOCUS_DOWN);
          }
    }

    @Override
    public void onProviderEnabled(String eProvider) {
        outputField.append("Provider " + eProvider + " has been enabled." + "\n");
        scrollView.fullScroll(ScrollView.FOCUS_DOWN);

        provider = locationManager.getBestProvider(criteria, true);
        locationManager.requestLocationUpdates(provider, 400, 1, this);

        if (provider != null) {
            outputField.append("Provider " + provider + " has been selected." + "\n");
            scrollView.fullScroll(ScrollView.FOCUS_DOWN);

            if (location != null) {
                onLocationChanged(location);
            } else {
                latituteField.setText("Location not available");
                longitudeField.setText("Location not available");
            }
          } else {
            outputField.append("No provider selected" + "\n");
            scrollView.fullScroll(ScrollView.FOCUS_DOWN);
          }
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        outputField.append("Provider " + provider + " status changed to: " + Integer.toString(status) + "\n");
        scrollView.fullScroll(ScrollView.FOCUS_DOWN);
    }
}
你是我的挚爱i 2024-10-09 07:51:18

从 API 级别 9 及更高版本开始,可以使用以下方法:

requestLocationUpdates(long minTime, float minDistance, Criteria criteria, PendingIntent intent);

有关使用的更多信息,请参阅文档。

From API level 9 and onwards, this is possible, using this method:

requestLocationUpdates(long minTime, float minDistance, Criteria criteria, PendingIntent intent);

See the documentation for more information on usage.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文