你好我是Android开发的新手我需要在后台任务中每15分钟获取一次用户位置我已经使用googleapiclient获取位置更新当gps关闭时如何使用融合api客户端获取位置更新有什么办法ola cabs使用的应用程序如何使用任何人帮助我让我发布我到目前为止尝试的代码:
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_HIGH_ACCURACY);
locationRequest.setInterval(1);
locationRequest.setFastestInterval(1);
fusedLocationProviderApi = LocationServices.FusedLocationApi;
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) {
if (mGAC != null) {
Location oLocation = LocationServices.FusedLocationApi.getLastLocation(mGAC);
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;
}
}
如果提前感谢,即使关闭gps,如何使其正常工作!!
答案 0 :(得分:0)
尝试
PRIORITY_LOW_POWER
而不是
PRIORITY_HIGH_ACCURACY
最后一个将尝试使用GPS上的网络位置。我不知道你在Manifest.xml中定义了什么,但如果你定义了
ACCESS_FINE_LOCATION
我会改变它
ACCESS_COARSE_LOCATION