gpt4 book ai didi

java - 在一项 Activity 中无法获取加速计传感器和 GPS 位置数据,问题出在哪里?

转载 作者:行者123 更新时间:2023-12-02 01:51:06 25 4
gpt4 key购买 nike

我正在尝试在一项 Activity 中同时获取 GPS 和加速计数据。试图将它们保存在两个不同的表中。加速度计数据没问题,我保存了它,但在使用位置监听器后,它不起作用,并且未提供 GPS 数据。

在 MANIFEST 中添加了权限。权限似乎没问题。

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) {

}
}

没有错误。未提供 GPS 数据。

最佳答案

可能的问题是首先检查并获得权限,然后注册位置监听器。检查下面的链接。问候。

stack overflow get user location

关于java - 在一项 Activity 中无法获取加速计传感器和 GPS 位置数据,问题出在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57425830/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com