为多个提供者使用 requestLocationUpdates

Posted

技术标签:

【中文标题】为多个提供者使用 requestLocationUpdates【英文标题】:Using requestLocationUpdates for more than one provider 【发布时间】:2011-05-08 05:16:04 【问题描述】:

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

有没有一种使用requestLocationUpdates 的方法,但以某种方式让它每次都能为我提供最佳活跃提供者的结果?我可以将getBestProvider 的结果传递给它,但它总是会从该提供者返回结果,如果用户打开/关闭gps,它就不会像我预期的那样工作。

我的代码在后台服务中。

【问题讨论】:

【参考方案1】:

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

不,尽管欢迎您为多个提供商分别注册 LocationListeners

我可以将 getBestProvider 的结果传递给它,但它总是会从该提供者返回结果,如果用户打开/关闭 gps,它就不会像我预期的那样工作。

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

【讨论】:

好的,我使用启用/禁用功能使它与不同的侦听器一起工作。谢谢【参考方案2】:

这就是我的工作:

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);
    

【讨论】:

纯金我的兄弟!纯金。可惜没有人投票给这个答案! 在 locationManager.requestLocationUpdates(provider, 400, 1, this) 中显示错误;它没有得到解决..为什么?【参考方案3】:

从 API 级别 9 起,可以使用以下方法:

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

See the documentation for more information on usage.

【讨论】:

以上是关于为多个提供者使用 requestLocationUpdates的主要内容,如果未能解决你的问题,请参考以下文章

我可以单独使用 Flask app.run() 为多个客户端提供服务吗?

使用 connect vhost 为多个 express.js 应用程序提供服务

具有相同证书的多个 APNS 提供商

在 Express 中使用客户端路由为多个 React 应用程序提供服务

多个数据库提供者的一个实体框架模型

Symfony 4:如何在防火墙中为用户/管理员提供多个提供者?