Android:FusedLocationApi在后台提供错误的位置
我正在为我的应用程序开发一个功能,该功能应该记录用户在后台的位置,并在稍后某个时候将其发送到服务器(将其视为一个简单的GPS跟踪器) 我正在使用FusedLocationApi,并通过挂起的意图请求位置传递 当我开始位置跟踪时,我确实会将位置广播发送到我的应用程序中,这样事情看起来就正常了。然而,当我长时间测试应用程序时,事情开始变得奇怪: 1) 当我在办公室时,我的设备连接到了Wi-Fi,似乎位置传送很好。位置准确无误,坐标差最小(电话在我桌上): 2) 当我开车离开办公室,我的测试手机与Wi-Fi断开连接时,位置仍然可以发送,但坐标始终相同(靠近我的办公室)。下面的日志是我在高速高速公路上驾驶时记录的(所有坐标都相同): 3) 过了一段时间(当我还在外面的时候),位置广播的传送完全停止了 4) 当我回到家中(手机连接到WiFi)时,位置广播将恢复传送,并传送正确的位置 我曾在两台设备上尝试过:Nexus 7“2013和Prestigio PAP 5500,其行为大体相同。当我远离Wi-Fi时,似乎无法正确传递位置。当然,GPS模块已打开(两台设备) 下面是我的代码供参考。LocationManager类驻留在服务中,该服务在我的测试期间一直运行(从日志中可以看到) 感谢您的帮助Android:FusedLocationApi在后台提供错误的位置,android,gps,Android,Gps,我正在为我的应用程序开发一个功能,该功能应该记录用户在后台的位置,并在稍后某个时候将其发送到服务器(将其视为一个简单的GPS跟踪器) 我正在使用FusedLocationApi,并通过挂起的意图请求位置传递 当我开始位置跟踪时,我确实会将位置广播发送到我的应用程序中,这样事情看起来就正常了。然而,当我长时间测试应用程序时,事情开始变得奇怪: 1) 当我在办公室时,我的设备连接到了Wi-Fi,似乎位置传送很好。位置准确无误,坐标差最小(电话在我桌上): 2) 当我开车离开办公室,我的测试手机与Wi
public class LocationManager extends BroadcastReceiver implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
private static LocationManager mInstance = null;
private GoogleApiClient mGoogleApiClient;
private PendingIntent locationReceivedPendingIntent;
public static LocationManager instance() {
if (mInstance == null) {
mInstance = new LocationManager();
}
return mInstance;
}
private LocationManager() {
mLog = LoggingManager.getLogger(getClass());
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(TimeDoctorApplication.context())
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(TimeTrackingModel.TASK_CHANGED);
LocalBroadcastManager.getInstance(TimeDoctorApplication.context()).registerReceiver(this, intentFilter);
locationReceivedPendingIntent = PendingIntent.getBroadcast(TimeDoctorApplication.instance().getApplicationContext(), 0, new Intent(LOCATION_RECEIVED), 0);
}
@Override
public void onConnected(@Nullable Bundle bundle) {
boolean noFLPermission = ActivityCompat.checkSelfPermission(TimeDoctorApplication.context(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED;
boolean noCLPermission = ActivityCompat.checkSelfPermission(TimeDoctorApplication.context(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED;
if (noCLPermission || noFLPermission) {
return;
}
LocationRequest locationRequest = LocationRequest.create();
locationRequest.setInterval(5 * 60 * 1000);
locationRequest.setFastestInterval(5 * 60 * 1000);
locationRequest.setMaxWaitTime(5 * 60 * 1000);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, locationRequest, locationReceivedPendingIntent);
}
@Override
public void onConnectionSuspended(int i) {}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {}
@Override
public void onReceive(Context context, Intent intent) {
switch (intent.getAction()) {
case SOME_EXTERNAL_EVENT:
int servicesAvailable = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(TimeDoctorApplication.context());
if (servicesAvailable != ConnectionResult.SUCCESS) {
GoogleApiAvailability.getInstance().showErrorNotification(TimeDoctorApplication.context(), servicesAvailable);
return;
}
if (NEEDS_TRACKING) {
if (mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
} else {
if (!mGoogleApiClient.isConnected()) {
mGoogleApiClient.connect();
}
}
break;
default:
break;
}
}
protected void recordLocation(Location l) {
//Processing location here
}
public static final String LOCATION_RECEIVED = "com.myapp.LOCATION_RECEIVED";
public static class LocationReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.hasExtra("com.google.android.location.LOCATION")) {
LocationManager.instance().recordLocation((Location) intent.getExtras().get("com.google.android.location.LOCATION"));
}
}
}
}
19:54:37.341 Location acquired, lat=53.9323538, lon=27.6920177
19:59:37.385 Location acquired, lat=53.9323538, lon=27.6920177
20:04:37.499 Location acquired, lat=53.9323538, lon=27.6920177
public class LocationManager extends BroadcastReceiver implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
private static LocationManager mInstance = null;
private GoogleApiClient mGoogleApiClient;
private PendingIntent locationReceivedPendingIntent;
public static LocationManager instance() {
if (mInstance == null) {
mInstance = new LocationManager();
}
return mInstance;
}
private LocationManager() {
mLog = LoggingManager.getLogger(getClass());
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(TimeDoctorApplication.context())
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(TimeTrackingModel.TASK_CHANGED);
LocalBroadcastManager.getInstance(TimeDoctorApplication.context()).registerReceiver(this, intentFilter);
locationReceivedPendingIntent = PendingIntent.getBroadcast(TimeDoctorApplication.instance().getApplicationContext(), 0, new Intent(LOCATION_RECEIVED), 0);
}
@Override
public void onConnected(@Nullable Bundle bundle) {
boolean noFLPermission = ActivityCompat.checkSelfPermission(TimeDoctorApplication.context(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED;
boolean noCLPermission = ActivityCompat.checkSelfPermission(TimeDoctorApplication.context(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED;
if (noCLPermission || noFLPermission) {
return;
}
LocationRequest locationRequest = LocationRequest.create();
locationRequest.setInterval(5 * 60 * 1000);
locationRequest.setFastestInterval(5 * 60 * 1000);
locationRequest.setMaxWaitTime(5 * 60 * 1000);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, locationRequest, locationReceivedPendingIntent);
}
@Override
public void onConnectionSuspended(int i) {}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {}
@Override
public void onReceive(Context context, Intent intent) {
switch (intent.getAction()) {
case SOME_EXTERNAL_EVENT:
int servicesAvailable = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(TimeDoctorApplication.context());
if (servicesAvailable != ConnectionResult.SUCCESS) {
GoogleApiAvailability.getInstance().showErrorNotification(TimeDoctorApplication.context(), servicesAvailable);
return;
}
if (NEEDS_TRACKING) {
if (mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
} else {
if (!mGoogleApiClient.isConnected()) {
mGoogleApiClient.connect();
}
}
break;
default:
break;
}
}
protected void recordLocation(Location l) {
//Processing location here
}
public static final String LOCATION_RECEIVED = "com.myapp.LOCATION_RECEIVED";
public static class LocationReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.hasExtra("com.google.android.location.LOCATION")) {
LocationManager.instance().recordLocation((Location) intent.getExtras().get("com.google.android.location.LOCATION"));
}
}
}