java - GPS状态监听器

标签 java android gps listener

我只是想听听 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)

最佳答案

这只是发生在我身上。

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

有两种:

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

和:android.location.LocationListener

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

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

关于java - GPS状态监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40519666/

相关文章:

android - 尝试查看和锁定卫星时 GPS 客户端中的时间

python - 将 GPS 坐标从有理分数转换为度数

Java使用xpath获取xml元素值 "And"

java - 带 Rest 的 Tomcat - HTTP 状态 500 - Servlet 执行抛出异常

java - 如何使用 apache POI 删除 xlsx 文件中的单元格内容

android - ExoPlayer 下载后从缓存/离线播放文件

android - React-Native:未启用 Hermes

android - 在Eclipse中编译Android App时出错?

java - 使用 openssl 和 -subj 参数在 Java 中生成 CSR

android - 通过eclipse重新安装apk文件后,getlastknownlocation总是返回null