0

我想从 Android 读取 GPS 数据。我已经按照在线教程编写了一些代码

public class MainActivity extends Activity
{

private DataCollection mDataCollection;

private LocationManager mLocationManager;
private Location mLocationGPS;
private String strLocationProvider = "";

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Config.mContext = this;
    mLocationManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
    mLocationGPS = getLocationProvider(mLocationManager);
    mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, locationListener);
}

protected void onResume() 
{
    super.onResume();
    //  mDataCollection.register();
    mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, locationListener);
}

protected void onPause() 
{
    super.onPause();
    //  mDataCollection.unregister();
    mLocationManager.removeUpdates(locationListener);
}

public void startCollection(View view)
{
    //  mDataCollection.register();
}

public void stopCollection(View view)
{
    //  mDataCollection.unregister();
}

private final LocationListener locationListener = new LocationListener() {
    public void onLocationChanged(Location location) {
      // Called when a new location is found by the network location provider.
        if (location != null)
        {
            double longitude = location.getLongitude();
        }
    }

    public void onStatusChanged(String provider, int status, Bundle extras) {}

    public void onProviderEnabled(String provider) {}

    public void onProviderDisabled(String provider) {}
  };

  private Location getLocationProvider(LocationManager lm){
        Location retLocation = null ;

        Criteria mCriteria01 = new Criteria();
        mCriteria01.setAccuracy(Criteria.ACCURACY_FINE);
        mCriteria01.setAltitudeRequired(false);
        mCriteria01.setBearingRequired(false);
        mCriteria01.setCostAllowed(true);
        mCriteria01.setPowerRequirement(Criteria.POWER_HIGH);

        strLocationProvider = lm.getBestProvider(mCriteria01, true);
        retLocation = lm.getLastKnownLocation(strLocationProvider);

        return retLocation;
   }
}

当我在 处设置断点时double longitude = location.getLongitude();,它永远不会在该断点处停止。我也在清单中包含了权限

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.collectdata"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

有人可以帮助我。提前致谢

4

1 回答 1

2

测试时您是否移动了至少 10 米?这就是“10”的意思mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, locationListener);

onCreate()此外,您在和中都不需要该行onResume()。只需从您的onCreate()方法中删除它(尽管它不应该引起问题,只是更整洁)。

http://developer.android.com/reference/android/location/LocationManager.html#requestLocationUpdates(long, float, android.location.Criteria, android.app.PendingIntent)

于 2013-09-25T03:56:11.663 回答