如果使用融合的位置API关闭GPS,如何获取位置

时间:2016-09-07 09:05:03

标签: android gps location fusedlocationproviderapi

如果GPS使用Fused API关闭,我试图获取设备lastknownlocation 但我的位置价值为零。

我的位置等级

public class FusedLocation implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {

    private static FusedLocation instance;
    private Context mContext;
    private static LocationRequest locationRequest;
    private static GoogleApiClient mGoogleApiClient;
    private Location mLastLocation;

    private static final long INTERVAL = 30 * 1000;//1000 * 60 * 30;
    private static final long FASTEST_INTERVAL = 15 * 1000;//1000 * 60 * 30;
    private static final long ONE_MIN = 1000 * 60;
    private static final long REFRESH_TIME = ONE_MIN * 5;
    private static final float MINIMUM_ACCURACY = 50.0f;

    public FusedLocation(Context context) {
        this.mContext = context;
        locationRequest = LocationRequest.create();
        locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        locationRequest.setInterval(INTERVAL);
        locationRequest.setFastestInterval(FASTEST_INTERVAL);

        buildGoogleApiClient();

        mGoogleApiClient.connect();

    }

    public static FusedLocation getInstance(Context context) {
        if (instance == null) {
            instance = new FusedLocation(context);
        }else{

            if(!mGoogleApiClient.isConnected()){
                mGoogleApiClient.connect();
            }
            System.out.println("NOT NULL VALUE");
        }
        return instance;
    }

    /**
     * Builds a GoogleApiClient. Uses the addApi() method to request the LocationServices API.
     */
    protected synchronized void buildGoogleApiClient() {
        mGoogleApiClient = new GoogleApiClient.Builder(mContext)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();

    }

    @Override
    public void onConnected(Bundle bundle) {
        Log.d("location:", "onConnected()");

        if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
        Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
        if (location != null) {
            mLastLocation = location;
            Log.d("onConnected", "lat:" + mLastLocation.getLatitude() + " and long:" + mLastLocation.getLongitude());

        } else {

            Log.d("failLocation", "No Location.");
        }
    }

    @Override
    public void onConnectionSuspended(int i) {
        if (!mGoogleApiClient.isConnected())
            mGoogleApiClient.connect();
    }

    @Override
    public void onLocationChanged(Location location) {

        mLastLocation = location;
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onProviderDisabled(String provider) {

    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {

    }

    public Location getLocation() {

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                    // TODO: Consider calling
                    //    ActivityCompat#requestPermissions
                    // here to request the missing permissions, and then overriding
                    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                    //                                          int[] grantResults)
                    // to handle the case where the user grants the permission. See the documentation
                    // for ActivityCompat#requestPermissions for more details.
                    return  ;
                }

                Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
                if (location != null) {
                    mLastLocation = location;
                    Log.d("getLocation", "lat:" + mLastLocation.getLatitude() + " and long:" + mLastLocation.getLongitude());
                } else {
                    //LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, FusedActivity.this);
                    Log.d("failLocation", "No Location.");
                }
            }
        },2000);
        return mLastLocation;
    }

}

0 个答案:

没有答案