即使我在位置请求上设置.interval,我的位置也不会刷新

时间:2017-02-28 09:00:52

标签: android location-services google-location-services

private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000;
private double currentLatitude;
private double currentLongitude;
private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;

Button btn_start;
TextView txt_refersh;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_demo);


    btn_start = (Button) findViewById(R.id.btn_start);
    txt_refersh = (TextView) findViewById(R.id.txt_refersh);


    btn_start.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Log.e("currentLatitude", "" + currentLatitude);
            Log.e("currentLongitude", "" + currentLongitude);



        }
    });


    mGoogleApiClient = new GoogleApiClient.Builder(this)
            // The next two lines tell the new client that “this” current class will handle connection stuff
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            //fourth line adds the LocationServices API endpoint from GooglePlayServices
            .addApi(LocationServices.API)
            .build();

    // Create the LocationRequest object
    mLocationRequest = LocationRequest.create()
            .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
            .setInterval(10 * 1000)        // 10 seconds, in milliseconds
            .setFastestInterval(1 * 1000); // 1 second, in milliseconds





}

@Override
protected void onResume() {
    System.runFinalization();
    Runtime.getRuntime().gc();
    System.gc();
    Log.e("System GC", "Called");


    super.onResume();


    //Now lets connect to the API
    mGoogleApiClient.connect();

}

@Override
protected void onStart() {

    super.onStart();
}

@Override
protected void onPause() {

    if (mGoogleApiClient.isConnected()) {
        LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
        mGoogleApiClient.disconnect();
    }


    super.onPause();
    Log.v(this.getClass().getSimpleName(), "onPause()");


}

@Override
public void onConnected(Bundle bundle) {
    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.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) {
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);

    } else {
        //If everything went fine lets get latitude and longitude
        currentLatitude = location.getLatitude();
        currentLongitude = location.getLongitude();


        LocationRequest locationRequest = LocationRequest.create();
        locationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
        locationRequest.setInterval(60000);

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, 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;
        }
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
        getLocationdetails(getApplicationContext());
        Log.e("currentLatitude", "" + currentLatitude);
        Log.e("currentLongitude", "" + currentLongitude);



    }
}

@Override
public void onConnectionSuspended(int i) {
}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
        /*
         * Google Play services can resolve some errors it detects.
         * If the error has a resolution, try sending an Intent to
         * start a Google Play services activity that can resolve
         * error.
         */
    if (connectionResult.hasResolution()) {
        try {
            // Start an Activity that tries to resolve the error
            connectionResult.startResolutionForResult(this, CONNECTION_FAILURE_RESOLUTION_REQUEST);
                /*
                 * Thrown if Google Play services canceled the original
                 * PendingIntent
                 */
        } catch (IntentSender.SendIntentException e) {
            // Log the error
            e.printStackTrace();
        }
    } else {
            /*
             * If no resolution is available, display a dialog to the
             * user with the error.
             */
        Log.e("Error", "Location services connection failed with code " + connectionResult.getErrorCode());
    }
}

/**
 * If locationChanges change lat and long
 *
 * @param location
 */
@Override
public void onLocationChanged(Location location) {
    currentLatitude = location.getLatitude();
    currentLongitude = location.getLongitude();
}

private Location getLocationdetails(Context context) {

    Location location = null;

    if (mGoogleApiClient != null) {


        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

            return null;


        } else {
            Log.e("currentLatitude", "" + currentLatitude);
            Log.e("currentLongitude", "" + currentLongitude);



            location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
        }


    }


    return location;
}

我使用locationRequest.setInterval(60000);刷新我的位置,但它仍然不令人耳目一新。我应该怎么做才能在10分钟后获得当前的纬度和经度。我只想在10分钟后得到lat的记录。

0 个答案:

没有答案