0

I am trying to get GPS and accelerometer data at same time in one activity. trying to save both of them in two different tables. accelerometer data is ok and I saved it but after having location listener it doesn't work and GPS data is not provided.

Added permissions in MANIFEST. Permissions seems to be ok.

public class MainActivity extends AppCompatActivity implements SensorEventListener {
    DatabaseHelper dbHelper;
    private SensorManager sensorManager;
    private Sensor linearAccelerometer;
    private LocationManager locationManager;
    private LocationListener locationListener;
    public float a;
    public float b;
    public float c;
    private final static String TAG = "MainActivityTest";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d(TAG, "onCreate: Initilizing Sensor Services");

        //LOCATION
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        locationListener = new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                Toast.makeText(getBaseContext(), "GPS Value Changed ", Toast.LENGTH_LONG).show();

                Log.d(TAG, "Latitude is   "+location.getLatitude() + "    Longitude is    "+ location.getLongitude());
            }

            @Override
            public void onStatusChanged(String s, int i, Bundle bundle) {

            }

            @Override
            public void onProviderEnabled(String s) {

            }

            @Override
            public void onProviderDisabled(String s) {

            }

        };
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                requestPermissions(new String[]{
                        Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.INTERNET
                },10);
            return;
        }
        }else
                {
                    locationManager.requestLocationUpdates("gps", 2000, 0, locationListener);

                }






        sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
linearAccelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
            // so we have accelerometer
            sensorManager.registerListener(MainActivity.this, linearAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
            Toast.makeText(getBaseContext(), "Sensor Registered ", Toast.LENGTH_LONG).show();
            Log.d(TAG, "Registered LINEAR ACC");
        }



    @Override
    public void onSensorChanged(SensorEvent sensorEvent) {
        Toast.makeText(getBaseContext(), "Sensor Value Changed ", Toast.LENGTH_LONG).show();
        Log.d(TAG, "X value is   "+ sensorEvent.values[0] + "    Y value is    "+ sensorEvent.values[1] + "    Z value is   " + sensorEvent.values[2]);


        //SQLITE INSERTATION
       Sensor sensor = sensorEvent.sensor;
       a = sensorEvent.values[0];
       b = sensorEvent.values[1];
       c = sensorEvent.values[2];

       if (sensor.getType() == Sensor.TYPE_ACCELEROMETER) {

            dbHelper.getInstance().insert(a, b, c);
       }

    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int i) {

    }
}

NO errros. GPS data not provided.

Anas Mehar
  • 2,739
  • 14
  • 25

1 Answers1

0

May problem is first, checking and get permission then register the location listener. Check the below link. Regards.

stack overflow get user location

  • check these: locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 0, locationListener); locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 2000, 0, locationListener); –  Aug 09 '19 at 08:42
  • what if comment accelerometer sensor codes and check location call backs working or not ? (only location) –  Aug 09 '19 at 09:15
  • No, still no GPS data. – Mehrdad Asadi Aug 10 '19 at 10:48
  • so the problem is about getting location and not related to accelerometer code. –  Aug 10 '19 at 11:10
  • Yes and that was my first question absolutely. – Mehrdad Asadi Aug 11 '19 at 10:20