I want to read GPS data from Android. I have written some code follow online tutorial
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;
}
}
When I put a break point at double longitude = location.getLongitude();
, it never stop at that break point. I also have include the permission in manifest
<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" />
Could someone help me. Thanks in advance
Are you moving at least 10 meters while testing this? That's what the "10" means in mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, locationListener);
Also, you don't need that line in both onCreate()
and onResume()
. Just remove it from your onCreate()
method (though it shouldn't cause problems, just is neater).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.