我有一个Android应用程序与谷歌地图和自动位置更新通过谷歌播放服务的新位置API。谷歌播放服务的位置停止位置更新的简历,无需拨打电话
实现就像这样: https://developer.android.com/training/location/receive-location-updates.html
我专门欲接收GPS /准确位置。
它正常工作100%,GPS图标位于上方,位置每隔几秒进入一次,无后顾之忧。
奇怪的问题似乎是,如果您切换到Google地图,请稍等,然后切换回我的应用程序,然后我的应用程序再获取一个位置更新,然后停止接收更新。
我的应用正在停止位置更新onPause/onStop,并重新连接并重新启动onStart/onResume。
我的调试Toast在从Google地图切换回来后显示“已连接”,并再次显示“已更新的位置”,然后停止更新。 onDisconnected()没有被调用,而mLocationClient.isConnected()的检查报告为'true'。
我已经添加了一个黑客解决方法,每隔几秒钟运行一次定时器处理程序,如果在最近10秒内未找到某个位置,它会在下面调用stopPauseLocation()和checkStartLocation()解决问题和地点再次开始。显然这是一个丑陋的黑客攻击,我对此并不满意。
它似乎就像一个错误,谷歌地图和我自己的应用程序之间有冲突,但是,我不能为我的生活找出一个真正的解决方案。
任何想法?
这是关键的代码片段:
@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();
}