【问题标题】:GPS Status ListenerGPS 状态监听器
【发布时间】:2016-11-10 03:49:52
【问题描述】:

我只是想听听 GPS 状态的变化。我有以下内容:

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

    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return;
    }

    lm.addGpsStatusListener(new android.location.GpsStatus.Listener() {

        public void onGpsStatusChanged(int event) {
            locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            switch (event) {
                case GPS_EVENT_STARTED:
                    if((alertDialog != null) && (alertDialog.isShowing())){
                        alertDialog.dismiss();
                    }
                    contador=0;
                    darHandle();
                    break;
                case GPS_EVENT_STOPPED:
                    if(mProgressDialog!=null && mProgressDialog.isShowing()) {
                        mProgressDialog.dismiss();
                    }
                    contador=0;
                    break;
            }
        }
    });

它在我的 Moto G 1st 中运行良好,但已被弃用并且在某些设备中无法运行。

我试图弄清楚如何在这种情况下使用onProviderEnabled()/onProviderDisabled()。我尝试了很多代码示例,但都没有成功。

我正在尝试这个:

LocationListener locationListenerGps = new LocationListener() {
        public void onLocationChanged(Location location) {

        }
        public void onProviderDisabled(String provider) {}
        public void onProviderEnabled(String provider) {}
        public void onStatusChanged(String provider, int status, Bundle extras) {}
    };

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

    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 12000, 0, locationListenerGps);

但在最后一行它说:

无法解析方法requestLocationUpdates(java.lang.String, int, int, com.google.android.gms.location.LocationListener)

【问题讨论】:

    标签: java android gps listener


    【解决方案1】:

    这只是发生在我身上。

    我认为您的问题必须与导入的侦听器的类有关。

    有两种:

    你使用的那个:com.google.android.gms.location.LocationListener

    和:android.location.LocationListener

    您使用的那些方法属于第二种。 使用那个,你应该会没事的。

    在Android中经常会发生这种事情,所以要小心,不同API的类有很多,其中一些重复名称,你必须尝试组合。

    【讨论】:

      猜你喜欢
      • 2017-05-31
      • 1970-01-01
      • 1970-01-01
      • 2015-05-23
      • 1970-01-01
      • 2021-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多