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.