即使使用融合位置API关闭gps,如何获取位置更新?

时间:2016-10-15 04:38:50

标签: android gps google-play-services fusedlocationproviderapi

这个问题可能会被问到很多次,但我找不到任何解决方案。 我需要让用户在后台的位置持续15分钟。

GPS打开时工作正常,但当GPS关闭时,应用程序没有获得位置更新。我需要它至少获得网络位置或wifi位置。

如何使用融合位置API实现此目的。

位置管理器具有此功能,但位于融合位置。由于Google建议开发人员使用Play服务API中的Location,因此我不想使用位置管理器。 我怎样才能实现这一点,让我发布到目前为止我所尝试的内容:

import android.content.Context;
import android.location.Location;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.util.Log;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.FusedLocationProviderApi;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;

/**
 * Created by 4264 on 14-10-2016.
 */

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


    private Location mlocation; // location
    private double latitude; // latitude
    private double longitude; // longitude
    private GoogleApiClient mGAC;
    private Context mContext;
    public static final String TAG = "GPSresource";
    private FusedLocationProviderApi fusedLocationProviderApi;
    private LocationRequest locationRequest;

    public Locationlistener(Context c)
    {
        mContext = c;
        try {
            buildGoogleApiClient();
            mGAC.connect();
        }
        catch(Exception e)
        {
            Log.d(TAG,e.toString());
        }
    }

    protected synchronized void buildGoogleApiClient() {
        locationRequest = LocationRequest.create();
        locationRequest.setPriority(LocationRequest.PRIORITY_LOW_POWER);
        locationRequest.setInterval(1);
        locationRequest.setFastestInterval(1);
        mGAC = new GoogleApiClient.Builder(mContext)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
       }


    public double getLatitude(){
        if(mlocation != null){
            latitude = mlocation.getLatitude();
        }

        // return latitude
        return latitude;
    }

    /**
     * Function to get longitude
     * */
    public double getLongitude() {
        if (mlocation != null) {
            longitude = mlocation.getLongitude();
        }

        // return longitude
        return longitude;
    }

    public Location GetLocationBlocking() throws InterruptedException {



        //      String lat=String.valueOf(moCurrentLocation.getLatitude());
        //    String longt=String.valueOf(moCurrentLocation.getLongitude());
//        Toast.makeText(oContext,"Lat"+lat+"long"+longt,Toast.LENGTH_SHORT).show();
        return mlocation;

    }


    @Override
    public void onConnected(Bundle bundle) {
            Location oLocation = LocationServices.FusedLocationApi.getLastLocation(mGAC);
            if (mGAC != null) {

                mlocation = oLocation;
            getLatitude();
            getLongitude();
            if (oLocation != null){
              Log.d("lat",String.valueOf(mlocation.getLatitude()));
            Log.d("long",String.valueOf(mlocation.getLongitude()));
            }
            else{
                LocationServices.FusedLocationApi.requestLocationUpdates(mGAC, locationRequest, this);
        }   }}


    @Override
    public void onConnectionSuspended(int i) {

    }




    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

    }

    @Override
    public void onLocationChanged(Location location) {
        mlocation=location;
    }
}

1 个答案:

答案 0 :(得分:2)

我不明白当你说你不想使用位置管理器和GPS时......看看这个Location Strategies ...基于这个doc我们有3个策略来访问位置:

  1. GPS
  2. 网络位置提供商
  3. 或同时使用
  4. 但是对于您的位置问题,这是我的提议: 在活动内部,输入以下内容以连接并开始接收位置更新:

    private GoogleApiClient mGoogleApiClient;
    private LocationRequest mLocationRequest;
    
    private long UPDATE_INTERVAL = 10 * 1000;  /* 10 secs */
    private long FASTEST_INTERVAL = 2000; /* 2 sec */
    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Create the location client to start receiving updates
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                              .addApi(LocationServices.API)
                              .addConnectionCallbacks(this)
                              .addOnConnectionFailedListener(this).build();
    }
    
    protected void onStart() {
        super.onStart();
        // Connect the client.
        mGoogleApiClient.connect();
    }
    
    protected void onStop() {
        // Disconnecting the client invalidates it.
        LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
    
        // only stop if it's connected, otherwise we crash
        if (mGoogleApiClient != null) {
            mGoogleApiClient.disconnect();
        }
        super.onStop();
    }
    
    public void onConnected(Bundle dataBundle) {
        // Get last known recent location. 
        Location mCurrentLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
        // Note that this can be NULL if last location isn't already known.
        if (mCurrentLocation != null) {
          // Print current location if not null
          Log.d("DEBUG", "current location: " + mCurrentLocation.toString());
          LatLng latLng = new LatLng(mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude());
        }
        // Begin polling for new location updates.
        startLocationUpdates();
    }
    
    @Override
    public void onConnectionSuspended(int i) {
        if (i == CAUSE_SERVICE_DISCONNECTED) {
          Toast.makeText(this, "Disconnected. Please re-connect.", Toast.LENGTH_SHORT).show();
        } else if (i == CAUSE_NETWORK_LOST) {
          Toast.makeText(this, "Network lost. Please re-connect.", Toast.LENGTH_SHORT).show();
        }
    }
    
    // Trigger new location updates at interval
    protected void startLocationUpdates() {
        // Create the location request
        mLocationRequest = LocationRequest.create()
            .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
            .setInterval(UPDATE_INTERVAL)
            .setFastestInterval(FASTEST_INTERVAL);
        // Request location updates
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,
            mLocationRequest, this);
    }
    

    然后使用onLocationChanged注册位置更新:

    public void onLocationChanged(Location location) {
        // New location has now been determined
        String msg = "Updated Location: " +
            Double.toString(location.getLatitude()) + "," +
            Double.toString(location.getLongitude());
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
        // You can now create a LatLng Object for use with maps
        LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
    }
    

    有关Fused Location API的更多信息,请参阅Make ypur app Location Aware 排查位置更新

    应始终使用GoogleApiClient利用LocationServices.API完成位置更新,如上所示。不要使用较不可靠的旧位置API。即使使用正确的FusedLocationApi,也有很多可能出错的事情。考虑以下潜在问题:

    • 您是否添加了必要的权限?确保您的应用具有此功能 INTERNETACCESS_COARSE_LOCATION权限以确保这一点 可以按照上面的指南中的说明访问位置。
    • 调用时是否为空? LocationServices.FusedLocationApi.getLastLocation这很正常 因为此方法仅在已有位置时才返回 最近被另一个应用程序检索如果返回null,则为此 意味着您需要开始接收位置更新 LocationServices.FusedLocationApi.requestLocationUpdates之前 收到如上所示的位置。
    • 您是否尝试在genymotion模拟器上获取位置?确保 您已启用GPS并正确配置了lat / lng。尝试重新启动 模拟器,如果需要,重新启用GPS或尝试设备(或 官方模拟器)而不是排除genymotion特定问题。