在没有冗余代码的情况下在多个活动中使用 google locationservices?

Using google locationservices in multiple activities without redundant the code?

大家好,我正在使用 Google 定位服务来获取用户位置。我正在使用 this 作为参考代码并且它有效 fine.But 问题是使用此服务我必须在所有活动中调用位置代码因为我需要我的应用程序中的整个用户位置但我不需要想要在所有活动中冗余此代码。那么有什么方法可以通过单个代码实例在我的应用程序中始终使用它。

请检查我在 Sandy 的回答后写的代码

public class App extends Application implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {
@Override
public void onCreate() {
    super.onCreate();
    mRequestingLocationUpdates = false;
    mLastUpdateTime = "";
    buildGoogleApiClient();

}
protected synchronized void buildGoogleApiClient() {
    Log.i(TAG, "Building GoogleApiClient");
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();
    mGoogleApiClient.connect();
    createLocationRequest();
}
protected void createLocationRequest() {

    mLocationRequest = new LocationRequest();

    // Sets the desired interval for active location updates. This interval is
    // inexact. You may not receive updates at all if no location sources are available, or
    // you may receive them slower than requested. You may also receive updates faster than
    // requested if other applications are requesting location at a faster interval.
    mLocationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS);

    // Sets the fastest rate for active location updates. This interval is exact, and your
    // application will never receive updates faster than this value.
    mLocationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);

    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
            .addLocationRequest(mLocationRequest);
    builder.setAlwaysShow(true); //this is the key ingredient

    PendingResult<LocationSettingsResult> result =
            LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, builder.build());
    result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
        @Override
        public void onResult(LocationSettingsResult result) {
            final Status status = result.getStatus();
            final LocationSettingsStates state = result.getLocationSettingsStates();
            Log.v("=========", "=====@@@@@@===requestCode=" + status.getStatusCode());
            switch (status.getStatusCode()) {
                case LocationSettingsStatusCodes.SUCCESS:
                    // All location settings are satisfied. The client can initialize location
                    // requests here.
                    startLocUpdate();

                    break;
                case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                    // Location settings are not satisfied. But could be fixed by showing the user
                    // a dialog.
                    try {
                        // Show the dialog by calling startResolutionForResult(),
                        // and check the result in onActivityResult().
                        status.startResolutionForResult(MainActivity.this, REQUEST_CHECK_SETTINGS);
                    } catch (IntentSender.SendIntentException e) {
                        // Ignore the error.
                    }
                    break;
                case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                    // Location settings are not satisfied. However, we have no way to fix the
                    // settings so we won't show the dialog.
                    break;
            }
        }
    });



}

}

创建一个扩展应用程序 Class 的 Class 如果您尚未创建,则编写所有逻辑以获取应用程序 class

上的位置

还在应用程序 class 上创建一个方法,returns 你可以从你引用的示例中获取一个位置对象

例如

public Location getLatestLocation(){
    return mLocation; //object that you got the location 

}

如有任何问题请告诉我