如何修复LocationManager和GpsStatus不返回卫星的问题

时间:2019-05-10 10:20:18

标签: java android android-gps

我正在尝试建立一项活动,当用户访问该活动时,它将告诉用户有多少可用的GPS卫星以及信号的强度,但是由于我什至无法获得卫星号,所以我有点卡住了。

现在,我已经花了一个多月的时间来尝试使其工作,方法是使代码在活动本身或多达几个扩展或实现GpsStatus,GpsStatus.Listener或LocationManager的类中完成,茫然的任何人有任何想法吗?

所有这些代码都在活动本身中。 我已经删除了所有尝试过的类和代码的使用方式,但是无法正常工作,这是我所获得的最接近的类,并且所有计数变量都不会返回零。

@TargetApi(Build.VERSION_CODES.N)
public void countSatellites(){

    int satellites = 0;
    int satellitesInFix = 0;

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

    GpsStatus status = locationManager.getGpsStatus(null);

    Iterable sats = status.getSatellites();
    Iterator satI = sats.iterator();
    int count = 0;

    while(satI.hasNext()){

        GpsSatellite gpssatellite = (GpsSatellite) satI.next();

        if (gpssatellite.usedInFix()){

            count++;

        }

    }


    int timetofix = locationManager.getGpsStatus(null).getTimeToFirstFix();


    for (GpsSatellite sat : locationManager.getGpsStatus(null).getSatellites()) {

        if (sat.usedInFix()) {

            satellitesInFix++;

        }

        satellites++;

    }


}

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {

    switch (requestCode) {

        case 1: {

            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                if (checkLocationPermission()) {

                    countSatellites();

                }

            } else {


            }

            return;

        }


    }

}


public boolean checkLocationPermission() {

    String permission = "android.permission.ACCESS_FINE_LOCATION";

    int res = this.checkCallingOrSelfPermission(permission);

    return (res == PackageManager.PERMISSION_GRANTED);

}


@TargetApi(Build.VERSION_CODES.M)
@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_connections);

    LocationUpdateService locationService = new LocationUpdateService();

    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);


}

在另一个文件中

public class LocationUpdateService extends Service implements GpsStatus.Listener{

    public void onCreate(){

        Log.i("on create", "success");

        super.onCreate();
        registerGpsStatusListener();

    }

    private void registerGpsStatusListener() {

        Log.i("gps listener", "success");

        ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);
        LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        locationManager.addGpsStatusListener(this);

    }

    @Override
    public void onGpsStatusChanged(int event) {

        Log.i("gps change", "success");

        switch (event) {

            case GpsStatus.GPS_EVENT_STARTED:

                Log.e("gps started", "onGpsStatusChanged started");
                break;

            case GpsStatus.GPS_EVENT_STOPPED:

                Log.e("gps stopped", "onGpsStatusChanged stopped");
                break;

            case GpsStatus.GPS_EVENT_FIRST_FIX:

                Log.e("gps first", "onGpsStatusChanged first fix");
                break;

            case GpsStatus.GPS_EVENT_SATELLITE_STATUS:

                Log.e("gps status", "onGpsStatusChanged status");
                break;

        }

    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.i("on bind", "success");
        return null;
    }


    public void onLocationChanged(Location location) {

    Log.i("location change", "success");

    }
}

0 个答案:

没有答案