我有这段代码来获取一些GNSS状态更新。我基本上是在检查代码是否要转到方法onSatelliteStatusChanged
import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.location.GnssMeasurement;
import android.location.GnssMeasurementsEvent;
import android.location.GnssStatus;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.UserHandle;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collection;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import static android.content.Context.LOCATION_SERVICE;
public class GNSSTestFragment extends Fragment implements LocationListener {
private final String TAG = "GNSS TEST";
private static final int REQUEST_LOCATION = 111;
private Context mContext;
private LocationManager mLocationManager;
private GnssStatus.Callback mGnssStatusCallback;
private TextView measurementTextView;
private final GnssMeasurementsEvent.Callback gnssMeasurementsEventListener =
new GnssMeasurementsEvent.Callback() {
@Override
public void onGnssMeasurementsReceived(GnssMeasurementsEvent event) {
Collection<GnssMeasurement> a = event.getMeasurements();
for(final GnssMeasurement gnssMeasurement: a){
if(gnssMeasurement.getConstellationType() == GnssStatus.CONSTELLATION_GPS) {
((Activity)mContext).runOnUiThread(new Runnable() {
@Override
public void run() {
measurementTextView.setText("Cn0 = " + gnssMeasurement.getCn0DbHz());
}
});
}
}
}
@Override
public void onStatusChanged(int status) {
}
};
private TextView numberSatellitesTextView;
private Handler mHandler = new Handler();
private boolean isLoadingProgress = false;
private static int gnssError = ErrorTest.TEST_NOT_WORKING;
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.gnss_test_fragment, container, false);
mContext = getContext();
numberSatellitesTextView = view.findViewById(R.id.number_satellites);
measurementTextView = view.findViewById(R.id.gnss_measurement);
measurementTextView.setMovementMethod(new ScrollingMovementMethod());
mLocationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);
new GnssMeasurementsEvent.Callback() {
@Override
public void onGnssMeasurementsReceived(GnssMeasurementsEvent event) {
event.getMeasurements();
}
};
mGnssStatusCallback = new GnssStatus.Callback() {
@Override
public void onSatelliteStatusChanged(GnssStatus status) {
if(getActivity()!=null) {
int numberGps = 0;
int numberGlonass = 0;
int numberSatellites = status.getSatelliteCount();
for (int i = 0; i < numberSatellites; i++) {
if (status.getConstellationType(i) == GnssStatus.CONSTELLATION_GPS || status.getConstellationType(i) == GnssStatus.CONSTELLATION_GLONASS ) {
numberGps++;
} else if (status.getConstellationType(i) == GnssStatus.CONSTELLATION_GLONASS) {
numberGlonass++;
}
String numberOfSatellites = getString(R.string.number_of_satelites) + numberSatellites + ".\n\n";
cn0/=numberGps;
String numberOfGps = getString(R.string.number_of_gps) + numberGps + " and Cn0 = "+cn0+".\n\n";
String numberOfGlonass = getString(R.string.number_of_glonass) + numberGlonass + ".";
numberSatellitesTextView.setText(numberOfSatellites + numberOfGps + numberOfGlonass);
isLoadingProgress = false;
gnssError = ErrorTest.TEST_WORKING;
}
}
@Override
public void onStarted(){
}
@Override
public void onStopped(){
}
};
return view;
}
@Override
public void onStart() {
super.onStart();
requestLocation();
}
private void requestLocation() {
if (mContext.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION);
}
else if(!mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
createDialogActivateGPSProvider();
}
else {
mLocationManager.registerGnssStatusCallback(mGnssStatusCallback,new Handler(Looper.getMainLooper()));
mLocationManager.registerGnssMeasurementsCallback(gnssMeasurementsEventListener);
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30000, 0, this);
}
}
@Override
public void onStop() {
mLocationManager.removeUpdates(this);
mLocationManager.unregisterGnssStatusCallback(mGnssStatusCallback);
mLocationManager.unregisterGnssMeasurementsCallback(gnssMeasurementsEventListener);
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) {
}
}
有时(大多数情况下),它运行良好,我可以看到流向方法onSatelliteStatusChanged
的流程,但是有时,它却从未流向该方法(位置设置为on)。
你知道为什么吗?