如何使用位置管理器“仅按需”获取Android当前位置但没有位置监听器?

时间:2016-03-09 16:52:27

标签: android location

我的应用不需要动态位置更新。只有在(1)应用程序启动时才需要该位置; (2)打开特定活动/片段; (3)用户点击“获取我的位置”按钮。

标准是:当要求时,即使粗略地(如果可能的话)最小电池消耗,该位置也必须快速。我计划使用以下方法:

public void getLocation() {
        LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

        Location location = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);

        if(location != null) {
            //save the location to shared prefs to use when needed
            return;
        }

        location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

        if(location != null) {
            //save the location to shared prefs to use when needed
            return;
        }

        location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

        if(location != null) {
            //save the location to shared prefs to use when needed
            return;
        }

        // if reached that point the location cannot be got!!
    }

计划是每次应用程序启动时调用该方法,或者激活特定页面,或者用户想要获取当前位置。除非这些操作触发位置更新,否则旧位置或没有位置就足够了。这种方法有意义吗?如果不实现Location Listener和/或调用方法“requestLocationUpdates()”,我可以这样做吗?我担心的是“getLastKnownLocation()”在调用时可能不会给出当前位置,因为我不确定提供者的“最后知道位置”的更新频率或条件是多少。

非常感谢任何建议和意见。

1 个答案:

答案 0 :(得分:1)

使用以下代码使用Google位置服务API获取当前和最后一个已知位置

//Global Variable
Location location;
public class MainActivity extends ActionBarActivity implements
    GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener,
    LocationListener {

   mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();

    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() {
    super.onResume();
    mGoogleApiClient.connect();
}

@Override
protected void onPause() {
    super.onPause();
    if (mGoogleApiClient.isConnected()) {
        LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
        mGoogleApiClient.disconnect();
    }
}

@Override
public void onConnected(Bundle bundle) {
    location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
    if (location == null) {
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
    } else {
        handleNewLocation(location);
    }
}

  @Override
public void onConnectionFailed(ConnectionResult connectionResult) {
    if (connectionResult.hasResolution()) {
        try {
            // Start an Activity that tries to resolve the error
            connectionResult.startResolutionForResult(this, CONNECTION_FAILURE_RESOLUTION_REQUEST);
        } catch (IntentSender.SendIntentException e) {
            e.printStackTrace();
        }
    } else {
        Log.i(TAG, "Location services connection failed with code " + connectionResult.getErrorCode());
    }
}

AnyActivity.class

MainActivity main = new MainActivity();
Location location = main.location;