gpt4 book ai didi

android - Google Play 服务位置在不调用 onDisconnected 的情况下停止恢复位置更新

转载 作者:行者123 更新时间:2023-11-29 00:25:56 24 4
gpt4 key购买 nike

我有一个带有 Google map 的 Android 应用程序,可以通过 Google Play 服务的新位置 API 自动更新位置。

实现方式如下: https://developer.android.com/training/location/receive-location-updates.html

我专门尝试接收 GPS/准确位置。

它 100% 完美正常地工作,GPS 图标在上面,每隔几秒就会出现一次位置,不用担心。

奇怪的问题似乎是,如果您切换到 Google map ,请稍等片刻,然后切换回我的应用程序,然后我的应用程序正好再获得一次位置更新,然后停止接收更新。

我的应用在暂停/停止时正确停止位置更新,并在开始/恢复时重新连接并重新启动它们。

我的调试 Toasts 在从 Google map 切换回来后显示“已连接”,并再显示一个“更新位置”,然后更新停止。 onDisconnected() 未被调用,并且 mLocationClient.isConnected() 的检查报告“真”。

从那以后,我添加了一个 hack work-around,它有一个每隔几秒运行一次的计时器处理程序,如果在过去 10 秒内没有找到位置,它会调用下面的 stopPauseLocation() 和 checkStartLocation(),这确实修复了问题和地点再次出现。显然,这是一个丑陋的 hack,我对此并不满意。

这似乎是一个错误,谷歌地图和我自己的应用程序之间存在冲突,但是,我终究无法找到真正的解决方案。

有什么想法吗?

关键代码 fragment 如下:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create the LocationRequest object
mLocationRequest = LocationRequest.create();
// Use high accuracy
mLocationRequest.setPriority(
LocationRequest.PRIORITY_HIGH_ACCURACY);
// Set the update interval to 2 seconds
mLocationRequest.setInterval(2000);
// Set the fastest update interval to 1 second
mLocationRequest.setFastestInterval(1000);
/*
* Create a new location client, using the enclosing class to
* handle callbacks.
*/
mLocationClient = new LocationClient(this, this, this);

mLocationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
}

@Override
protected void onStart() {
super.onStart();

// Check our connection to play services
checkStartLocation();
}

/*
* Called when the Activity is no longer visible at all.
* Stop updates and disconnect.
*/
@Override
protected void onStop() {
stopPauseLocation();
}

/*
* Called by Location Services when the request to connect the
* client finishes successfully. At this point, you can
* request the current location or start periodic updates
*/
@Override
public void onConnected(Bundle dataBundle) {
// Display the connection status
Toast.makeText(this, "Connected", Toast.LENGTH_SHORT).show();
mLocationClient.requestLocationUpdates(mLocationRequest, this);

super.onStop();
}

private void stopPauseLocation()
{
// If the client is connected
if (mLocationClient.isConnected()) {
/*
* Remove location updates for a listener.
* The current Activity is the listener, so
* the argument is "this".
*/
mLocationClient.removeLocationUpdates(this);
}
/*
* After disconnect() is called, the client is
* considered "dead".
*/
mLocationClient.disconnect();
}

/**
* Helper to check if we're connected to play, and try to connect if not
*/
protected void checkStartLocation() {
if (!mLocationClient.isConnected())
{
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationClient.connect();
}
}

/*
* Called by Location Services when the request to connect the
* client finishes successfully. At this point, you can
* request the current location or start periodic updates
*/
@Override
public void onConnected(Bundle dataBundle) {
// Display the connection status
Toast.makeText(this, "Connected", Toast.LENGTH_SHORT).show();
mLocationClient.requestLocationUpdates(mLocationRequest, this);
}

@Override
public void onDisconnected() {
// Display the connection status
Toast.makeText(this, "Disconnected.",Toast.LENGTH_SHORT).show();
}

/*
* Called by Location Services if the attempt to connect to
* Location Services fails.
*/
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Toast.makeText(this, "onConnectionFailed", Toast.LENGTH_SHORT).show();
}

// Define the callback method that receives location updates
@Override
public void onLocationChanged(Location location) {
// Report to the UI that the location was updated
String msg = "Updated Location: " +
Double.toString(location.getLatitude()) + "," +
Double.toString(location.getLongitude());
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}

最佳答案

我找到的唯一解决方法是黑客攻击,但这是我想出的最好的黑客攻击版本。

下面的代码将每 10 秒(然后逐渐增加 15 秒、20 秒、最多 30 秒)检查一次位置更新。如果未收到任何位置,它会调用 removeLocationUpdates() 和 requestLocationUpdates(),这似乎解决了我的问题。

private Handler locationCheck = null;
// Track the time of the last location update
private long lastLocationUpdate = System.currentTimeMillis();
private long lastLocationWaitTime = 0;
// How long to wait until we reconnect (10 sec)
private long WAIT_LOCATION_AGE_START = 10000;
// Increments of how much longer to wait before next check
// Increments on every failure to give the system more time to recover
private long WAIT_LOCATION_AGE_INCREMENT = 5000;
// Max time to wait
private long MAX_WAIT_LOCATION_AGE = 30000;

Add to onCreate in your class:
locationCheck = new Handler();

Add to onResume/onStart:
lastLocationUpdate = System.currentTimeMillis();
lastLocationWaitTime = WAIT_LOCATION_AGE_START;
locationCheck.removeCallbacks(locationCheckRunnable);
locationCheck.postDelayed(locationCheckRunnable, 1000);

Add to onStop/onPause:
locationCheck.removeCallbacks(locationCheckRunnable);

private Runnable locationCheckRunnable = new Runnable() {
@Override
public void run() {
if ((System.currentTimeMillis() - lastLocationUpdate) > lastLocationWaitTime)
{
// Verify our connection
checkStartLocation();
// Reset the timer
lastLocationUpdate = System.currentTimeMillis();
// On next check wait a bit longer
lastLocationWaitTime += WAIT_LOCATION_AGE_INCREMENT;
lastLocationWaitTime = Math.min(lastLocationWaitTime, MAX_WAIT_LOCATION_AGE);
// Re-request location updates
mLocationClient.removeLocationUpdates(MyParentClassName.this);
mLocationClient.requestLocationUpdates(mLocationRequest, LocationSocialBaseScreen.this);
}

locationCheck.postDelayed(this, 1000);
}
};

@Override
public void onLocationChanged(Location location) {
lastLocationUpdate = System.currentTimeMillis();
}

关于android - Google Play 服务位置在不调用 onDisconnected 的情况下停止恢复位置更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19477329/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com