2

这个问题可能会问很多时间,但我找不到任何解决方案。 我需要在后台获取用户的位置,历时15分钟。即使使用融合位置API关闭gps,如何获取位置更新?

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

如何使用融合位置API实现此目标?

位置管理器具有此功能,但在融合的位置。因为Google建议开发者使用Play服务API中的位置信息,所以我不想使用位置管理器。 我怎样才能做到这一点,所以让我寄我试过到目前为止:

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; 
    } 
} 
+0

看到.http://stackoverflow.com/questions/34305377/get-current-location-0-in-marshmallow-where-below-23-api-its-give-exact-current –

回答

1

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

  1. 全球定位系统
  2. 网络位置提供商
  3. 或同时使用

但对于您的位置问题,这是我的建议: 内的活动,把下面的连接并开始接收位置更新:

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()); 
} 

有关熔合位置API的更多信息,请参阅Make ypur app Location Aware 故障排除位置更新

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

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

它应该在不与用户交互的情况下在后台运行,那么如何与活动连接 –