user2753594
user2753594

Reputation: 145

How to read GPS data and accelerometer data in one activity

Is it possible the read GPS data and accelerometer data in one activity? I Want to read the data and put in the text view. I have already read the accelerometer data but the application crash when I add this code in Activity

public class MainActivity extends Activity implements SensorEventListener {
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    valuesInitialization();
    sensorsInitialization();

}

private void valuesInitialization()
{
    Config.mContext = this;
    mInitialized = false;
    mLastAccelerometer[0] = INITVALUE;
    mLastAccelerometer[1] = INITVALUE;
    mLastAccelerometer[2] = INITVALUE;
    mLastGyroscope[0] = INITVALUE;
    mLastGyroscope[1] = INITVALUE;
    mLastGyroscope[2] = INITVALUE;
    mLastOrientation[0] = INITVALUE;
    mLastOrientation[1] = INITVALUE;
    mLastOrientation[2] = INITVALUE;
}

private void sensorsInitialization() 
{
    mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION);
    mGyroscope = mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
    mOrientation = mSensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR);

    mLocationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
}

private void registListener() 
{
    mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
    mSensorManager.registerListener(this, mGyroscope, SensorManager.SENSOR_DELAY_NORMAL);
    mSensorManager.registerListener(this, mOrientation, SensorManager.SENSOR_DELAY_NORMAL);
    mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mLocationListener);
}

protected void onResume() 
{
    super.onResume();
    registListener();
}

protected void onPause() 
{
    super.onPause();
    mSensorManager.unregisterListener(this);
    }
}

Is there any way to solve the problem? Thanks in advance

The Error Log:enter image description here enter image description here

Upvotes: 1

Views: 3280

Answers (3)

minmaxavg
minmaxavg

Reputation: 696

At registListener (spelling incorrect) :

private void registListener() 
{
    mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
    mSensorManager.registerListener(this, mGyroscope, SensorManager.SENSOR_DELAY_NORMAL);
    mSensorManager.registerListener(this, mOrientation, SensorManager.SENSOR_DELAY_NORMAL);
    mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mLocationListener);
}

The error occurs because mLocationListener is null.

You should implement LocationListener in your own Activity, and in mLocationManager.requestLocationUpdates pass this as the listener.

EDIT: If you implement LocationListener located at android.location, You would override these methods:

public void onLocationChanged (Location location);
public void onProviderEnabled (String provider);
public void onProviderDisabled (String provider);
public void onStatusChanged (String provider, int status, Bundle extras)

onLocationChanged is called when the user move distance that is equal to or above the distance specified in third parameter of mLocationManager.requestLocationUpdates, and when the time is elapsed more than the time specified in second parameter, in milliseconds. You can use the location object as whatever you want.

onProviderEnabled is called when one of the providers that provide GPS location is enabled. You can retrieve the type of provider (it could be LocationManager.GPS_PROVIDER for GPS, or LocationManager.NETWORK_PROVIDER for Wi-Fi or cellular location).

onProviderDisabled is similiar to onProviderEnabled but it is called when the provider is disabled.

and finally, all methods including onStatusChanged is documented on here

Location class is in package android.location, which provides current user location in altitude, bearing, speed, time, extras, latitude, longitude, location provider and accuracy.

Upvotes: 1

Code_Yoga
Code_Yoga

Reputation: 3248

You must Implement a Location Listener in your class, as shown below

public class LocationListener implements android.location.LocationListener {
    final String LOG_LABEL = "Location Listener>>";

    @Override
    public void onLocationChanged(Location location) {
        Log.d(util.TAG,LOG_LABEL+ "Location Changed");
        if (location != null)
        {
            double longitude = location.getLongitude();
            Log.d(util.TAG,LOG_LABEL+ "Longitude:" + longitude);
            Toast.makeText(getApplicationContext(),"Long::"+longitude,Toast.LENGTH_SHORT).show();
            double latitude = location.getLatitude();
            Toast.makeText(getApplicationContext(),"Lat::"+latitude,Toast.LENGTH_SHORT).show();
            Log.d(util.TAG,LOG_LABEL+ "Latitude:" + latitude);

            cleanUp();

        }
    }

And also define a location listener as shown below

private android.location.LocationListener locListener;

Then in order to get the listener get started , do

this.locListener = new LocationListener();

And once you get the long/lat values, do not forget to a clean up as shown below

cleanUp()
{
     // This needs to be done to stop getting the location data and save the
    // battery power.
    if (null != this.locListener && null != locationManager)
    {
        locationManager.removeUpdates(this.locListener);
        this.locListener = null;
    }
}

Upvotes: 2

loose11
loose11

Reputation: 627

Did you have added the permission for GPS in the Manifest.xml?

Upvotes: 2

Related Questions