【问题标题】:LocationListener Android位置监听器 Android
【发布时间】:2015-08-25 05:56:48
【问题描述】:

我正在按照 Android Developer 上的指南检索位置,并试图从中获得速度。我按照指南编写了这段代码,但日志没有打印,文本也没有更新。

        final Button button = (Button) findViewById(R.id.button3);
       final TextView speed = (TextView) findViewById(R.id.textView5);

// Acquire a reference to the system Location Manager
        final LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
        final String locationProvider = LocationManager.GPS_PROVIDER;
// Define a listener that responds to location updates
        final LocationListener locationListener = new LocationListener() {
            public void onLocationChanged(Location location) {
                // Called when a new location is found by the network location provider.
                float getS=(location.getSpeed());
                speed.setText("Speed: "+getS);
            }

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

                Log.e("",""+status+" "+provider);
            }

            public void onProviderEnabled(String provider) {

            }

            public void onProviderDisabled(String provider) {}
        };


        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (running == 0) {
                    running = 1;
                    button.setText("Stop measuring");
                    locationManager.requestLocationUpdates(locationProvider, 0, 0, locationListener);

                } else {
                    running = 0;
                    button.setText("Start measuring");
                    locationManager.removeUpdates(locationListener);
                }
            }
        });

【问题讨论】:

标签: android gps


【解决方案1】:

代码中似乎没有问题。您正在使用 GPS_PROVODER 获取位置。从 GPS 获取位置可能存在问题。您应首先验证 GPS 在您的设备上是否正常工作。您可以通过使用 Google 地图应用程序并在移动设备时检查位置来做到这一点。

【讨论】:

  • 设置登录onLocationChanged(...),然后查看是否被调用。
  • 没有被调用,嗯
  • 写下这一行 locationManager.requestLocationUpdates(locationProvider, 0, 0, locationListener);在这个最后的字符串 locationProvider = LocationManager.GPS_PROVIDER; 下面
  • 给出空指针,无效的监听器。可能是因为监听器还没有被启动
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-05
  • 1970-01-01
  • 2019-08-17
相关资源
最近更新 更多