這個問題可能會問很多時間,但我找不到任何解決方案。 我需要在後臺獲取用戶的位置,歷時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;
}
}
看到.http://stackoverflow.com/questions/34305377/get-current-location-0-in-marshmallow-where-below-23-api-its-give-exact-current –