没有获得活动中的位置更新

时间:2014-04-13 18:09:09

标签: android location

我正在尝试使用developer.android.com here中的代码,但我无法收到位置更新。它只显示一次然后停止的位置。这是我的代码。请告诉我我做错了什么

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();
}
   }

1 个答案:

答案 0 :(得分:0)

快速提议:

  

public LocationRequest setInterval(long millis)

     

此间隔不准确。您可能根本没有收到更新(如果没有可用的位置源),或者您收到的速度比请求的速度慢。

您确定要获取位置数据吗?

<强> UPD

在此代码中,字段mUpdatesRequested永远不会设置为true。清理首选项(清除设置中的应用数据)并在mUpdatesRequested = true;中设置onCreate并在onResume方法中注释代码,因为它始终将mUpdatesRequested设置为false。或者事先保存具有真值的首选项。