Android 未在活动中获取位置更新
我正在尝试developer.android.com上的代码,但无法接收位置更新。它只显示一次位置,然后停止。这是我的密码。请告诉我我做错了什么Android 未在活动中获取位置更新,android,location,Android,Location,我正在尝试developer.android.com上的代码,但无法接收位置更新。它只显示一次位置,然后停止。这是我的密码。请告诉我我做错了什么 public class TestClass extends FragmentActivity implements GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedListener, LocationLis
public class TestClass extends FragmentActivity implements
GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener, LocationListener {
LocationClient mLocationClient;
Location mLocation;
boolean mUpdatesRequested;
// Milliseconds per second
private static final int MILLISECONDS_PER_SECOND = 1000;
// Update frequency in seconds
public static final int UPDATE_INTERVAL_IN_SECONDS = 5;
// Update frequency in milliseconds
private static final long UPDATE_INTERVAL = MILLISECONDS_PER_SECOND
* UPDATE_INTERVAL_IN_SECONDS;
// The fastest update frequency, in seconds
private static final int FASTEST_INTERVAL_IN_SECONDS = 1;
// A fast frequency ceiling in milliseconds
private static final long FASTEST_INTERVAL = MILLISECONDS_PER_SECOND
* FASTEST_INTERVAL_IN_SECONDS;
// Define an object that holds accuracy and frequency parameters
LocationRequest mLocationRequest;
SharedPreferences mPrefs;
Editor mEditor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_main);
mLocationClient = new LocationClient(this, this, this);
mLocationRequest = LocationRequest.create();
// Use high accuracy
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
// Set the update interval to 5 seconds
mLocationRequest.setInterval(UPDATE_INTERVAL);
// Set the fastest update interval to 1 second
mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
// Open the shared preferences
mPrefs = getSharedPreferences("SharedPreferences", Context.MODE_PRIVATE);
// Get a SharedPreferences editor
mEditor = mPrefs.edit();
/*
* Create a new location client, using the enclosing class to handle
* callbacks.
*/
mLocationClient = new LocationClient(this, this, this);
// Start with updates turned off
mUpdatesRequested = false;
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
mLocationClient.connect();
}
@Override
protected void onPause() {
// Save the current setting for updates
mEditor.putBoolean("KEY_UPDATES_ON", mUpdatesRequested);
mEditor.commit();
super.onPause();
}
@Override
protected void onResume() {
/*
* Get any previous setting for location updates Gets "false" if an
* error occurs
*/
super.onResume();
if (mPrefs.contains("KEY_UPDATES_ON")) {
mUpdatesRequested = mPrefs.getBoolean("KEY_UPDATES_ON", false);
// Otherwise, turn off location updates
} else {
mEditor.putBoolean("KEY_UPDATES_ON", false);
mEditor.commit();
}
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
if (mLocationClient.isConnected()) {
/*
* Remove location updates for a listener. The current Activity is
* the listener, so the argument is "this".
*/
removeLocationUpdates(this);
}
/*
* After disconnect() is called, the client is considered "dead".
*/
mLocationClient.disconnect();
super.onStop();
}
private void removeLocationUpdates(TestClass testClass) {
// TODO Auto-generated method stub
}
@Override
public void onConnectionFailed(ConnectionResult arg0) {
// TODO Auto-generated method stub
}
@Override
public void onConnected(Bundle arg0) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Connected", Toast.LENGTH_LONG)
.show();
mLocation = mLocationClient.getLastLocation();
Toast.makeText(getApplicationContext(), "One " + mLocation + " Two",
Toast.LENGTH_LONG).show();
}
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();
// If already requested, start periodic updates
if (mUpdatesRequested) {
mLocationClient.requestLocationUpdates(mLocationRequest, this);
}
}
@Override
public void onDisconnected() {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Disconnected",
Toast.LENGTH_LONG).show();
}
}
作为一项快速建议:
公共位置请求设置间隔(长毫秒)
这个间隔是不精确的。您可能根本接收不到更新(如果没有可用的位置源),或者您接收更新的速度可能比请求的慢
您确定正在获取位置数据吗
UPD
字段
mUpdatesRequested
在此代码中从未设置为true。清除prefs(清除设置中的应用程序数据)并设置mUpdatesRequested=true在onCreate
中的code>并在onResume
方法中注释代码,因为它总是将mUpdatesRequested
设置为false。或者事先用真值保存pref。我在第一次加载活动时得到它。那么我应该将其更改为什么?第一次位置管理器向您提供最后已知的位置数据,之后您不会收到任何更新,因为您没有任何可用的位置源。这是我的建议。比如说,你们有gps吗?哦!在我看来,mUpdatesRequested
从未设置为true:)我在声明时将其设置为true。仍然不起作用