gpt4 book ai didi

java - GPS状态监听器

转载 作者:太空宇宙 更新时间:2023-11-04 12:04:40 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com