GnssStatus.CallBack onSatelliteStatusChanged()无法正常工作 [英] GnssStatus.CallBack onSatelliteStatusChanged() not working
问题描述
我正在开发一个想要了解gps卫星计数的应用程序.我为此使用了"onSatelliteStatusChanged"方法,但是它不起作用.您在下面看到的我使用的这段代码.
I am developing an application that I want to learn about the count of gps satellites. I am using the "onSatelliteStatusChanged" method for this, but it does not work. The piece of code that I use below that you see.
代码
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
gnssStatusCallBack = new GnssStatus.Callback() {
@Override
public void onSatelliteStatusChanged(GnssStatus status) {
satelliteCount = status.getSatelliteCount();
}
};
locManager.registerGnssStatusCallback(gnssStatusCallBack);
} else {
locManager.addGpsStatusListener(this);
}
}
注意:我在外面测试过.
Note: I tested it outside.
推荐答案
概述(请求位置更新以接收它们):
如果未明确要求更新位置,则您的设备可能未在寻找此信息.以下对我有用,其中成员mGnssStatusCallback
是您的 LocationListener ;在这里,此活动实现了它,我将this
作为最终参数传递给requestLocationUpdates
和removeUpdates
. 30000
表示我们每30秒(30000 milli 秒)请求更新,而0
表示我们仅在设备移动> 0米时才需要更新.
您可能需要弄清楚哪些供应商可用,例如等.
请参见Android培训.
Overview (request location updates to receive them):
Without explicitly asking for location updates, your device may not be seeking this information. The following worked for me where member mGnssStatusCallback
is your GnssStatus.Callback and the Activity (or some other class) implements LocationListener; here this activity implements it and I pass this
as the final parameter to requestLocationUpdates
and removeUpdates
. The 30000
indicates that we're requesting updates every 30 seconds (30000 milliseconds), with 0
indicating that we only want an update if the device has moved > 0 meters.
You probably need to figure out what providers are available, etc.
see Android Training.
public class GnssActivity extends Activity implements LocationListener {
GnssStatus.Callback mGnssStatusCallback;
LocationManager mLocationManager;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mLocationManager =
(LocationManager) getSystemService(LOCATION_SERVICE);
mGnssStatusCallback = new GnssStatus.Callback() {
// TODO: add your code here!
};
}
@Override
protected void onStart() {
super.onStart();
mLocationManager.registerGnssStatusCallback(mGnssStatusCallback);
mLocationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 30000, 0, this
);
}
@Override
protected void onStop() {
mLocationManager.removeUpdates(this);
mLocationManager.unregisterGnssStatusCallback(
mGnssStatusCallback
);
super.onStop()
}
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
⋮
}
这篇关于GnssStatus.CallBack onSatelliteStatusChanged()无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!