Android GPS not working for me

2019-09-05 10:22发布

问题:

My app has two activities that need GPS, so I tried to offload it to a separate class that either activity could use. I found an answer here that looked easy enough Android - Best way to implement LocationListener across multiple activities But of course, it's not working for me. I was wondering if anyone can see the issue. I used pretty much exactly the same code, but I got rid of the gps settings dialog.

Here's my GPS class

package fieldlayout.skipmorrow.com.fieldlayout;

import android.app.Activity;
import android.content.Context;
import android.location.LocationManager;
import android.os.Bundle;

import android.location.LocationListener;

import android.location.Location;
import android.util.Log;


/**
 * Created by skip on 4/20/2015.
 */
public class GPS {
    private IGPSActivity main;

    // Helper for GPS-Position
    private LocationListener mlocListener;
    private LocationManager mlocManager;

    private boolean isRunning;

    public GPS(IGPSActivity main) {
        this.main = main;

        // GPS Position
        mlocManager = (LocationManager) ((Activity) this.main).getSystemService(Context.LOCATION_SERVICE);
        mlocListener = new MyLocationListener();
        mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
        // GPS Position END
        this.isRunning = true;
        Log.i("FieldLayout_GPS", "GPS Object created");
    }

    public void stopGPS() {
        if(isRunning) {
            mlocManager.removeUpdates(mlocListener);
            this.isRunning = false;
        }
        Log.i("FieldLayout_GPS", "stopGPS");
    }

    public void resumeGPS() {
        mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
        this.isRunning = true;
        Log.i("FieldLayout_GPS", "resumeGPS");
    }

    public boolean isRunning() {
        return this.isRunning;
    }

    public class MyLocationListener implements LocationListener {

        private final String TAG = MyLocationListener.class.getSimpleName();

        @Override
        public void onLocationChanged(Location loc) {
            GPS.this.main.locationChanged(loc.getLongitude(), loc.getLatitude());
            Log.i("FieldLayout_GPS", "onLocationChanged");
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            Log.i("FieldLayout_GPS", "onStatusChanged");
        }

        @Override
        public void onProviderEnabled(String provider) {
            Log.i("FieldLayout_GPS", "onProviderEnabled");
        }

        @Override
        public void onProviderDisabled(String provider) {
            Log.i("FieldLayout_GPS", "onProviderDisabled");
        }
    }

}

The interface file

package fieldlayout.skipmorrow.com.fieldlayout;

/**
 * Created by skip on 4/20/2015.
 */
public interface IGPSActivity {
    public void locationChanged(double longitude, double latitude);
}

And my implementation from my activity

package fieldlayout.skipmorrow.com.fieldlayout;

import android.content.Context;
import android.content.Intent;


public class StartActivity extends ActionBarActivity implements IGPSActivity{

    private Location currentLocation;
    private GPS gps;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start);
        gps = new GPS(this);
    }

    @Override
    protected void onResume() {
        if (!gps.isRunning()) gps.resumeGPS();
        super.onResume();
    }

    @Override
    protected void onStop() {
        // Disconnecting the client invalidates it.
        Log.i("FieldLayout_StartAct", "onStop called. Disconnecting GPS client");
        gps.stopGPS();
        super.onStop();
    }

    @Override
    public void locationChanged(double longitude, double latitude) {
        Log.i("FieldLayout_StartAct", "locationChanged");
        currentLocation.setLatitude(latitude);
        currentLocation.setLongitude(longitude);
    }
}

The only log I am seeing is the creation of the GPS object. None of the other methods in the listener are being executed.

回答1:

GPS class constructor contains this row:

 public GPS(IGPSActivity main) {
    this.main = main;

    // GPS Position
    mlocManager = (LocationManager) ((Activity) this.main).getSystemService(Context.LOCATION_SERVICE);
...
}

And your interface looks like this:

public interface IGPSActivity {
    public void locationChanged(double longitude, double latitude);

}

So, the GPS class get an interface, and you want to cast it into an Activity to get a system service from it. That is not going to work.

Change your GPS class a little bit. For example:

public GPS(IGPSActivity main, Activity activity){
    mlocManager = (LocationManager) activity.getSystemService(Context.LOCATION_SERVICE);
...
}

Call this in your activity:

gps = new GPS(this, this);


标签: android gps