我正在处理一个应用程序,其中的一部分是估计用户所在建筑物的楼层。我试图在设备上使用气压计/压力传感器,支持它并试图根据这个做出估计。如果用户没有气压计/压力传感器,应用程序会打开一个手动输入对话框,否则估计高度和楼层数量。压力传感器没有得到任何值
现在,我只是想让气压计/压力传感器工作。我使用的是具有可设定压力值的Android模拟器,和我的代码的相关部分被设置如下:
MainActivity.java
public class MainActivity extends AppCompatActivity implements SensorEventListener, .... {
//sensor variables
public float mPressureValue = 0.0f;
public float mHeight = 0.0f;
public Integer pressureBasedFloor = 0;
//check if device has pressure sensor, setup in OnCreate
boolean hasBarometer = false;
//pressure sensor to get pressure, height and floor
@Override
public void onSensorChanged(SensorEvent event) {
//if you use this listener as listener of only one sensor (ex, Pressure), then you don't need to check sensor type.
//if a pressure sensor exists, use it to calculate height
if (hasBarometer) {
if(Sensor.TYPE_PRESSURE == event.sensor.getType()) {
mPressureValue = event.values[0];
System.out.println("Pressure" + mPressureValue);
mHeight = SensorManager.getAltitude(SensorManager.PRESSURE_STANDARD_ATMOSPHERE, mPressureValue);
System.out.println("Height" + mHeight);
pressureBasedFloor = Math.round(mHeight);
}
}
}
//pressure sensor
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
@Override
protected void onCreate(Bundle savedInstanceState) {
//check if barometer sensor exists
hasBarometer = getPackageManager().hasSystemFeature(PackageManager.FEATURE_SENSOR_BAROMETER);
final FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//if user has no barometer sensor, request dialog
if (!hasBarometer) {
showRequestFloorDialog(view); //no barometer found, manual entry
} else {
currUserData.setUserFloorNumber(pressureBasedFloor); //barometer found, add calculated floor number to field
}
}
}
});
}
}
currUserData是一个自定义的一个实例class UserData存储用户的估计楼层号。我的清单文件设置如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.flamedra.findmycar">
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-feature android:name="android.hardware.sensor.pressure" android:required="true" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_profile"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:configChanges="orientation|keyboardHidden|screenSize">
<activity
android:name=".activities.MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar"
android:configChanges="orientation|keyboardHidden|screenSize"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".activities.CompassFragmentActivity"
android:theme="@style/AppTheme.NoActionBar"
android:configChanges="orientation|keyboardHidden|screenSize"
/>
<!--Maps Android Key-->
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_api_key" />
<!--Maps Android Key-->
</application>
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
</manifest>
眼下,pressureBasedFloor的整个价值即使在我的仿真器的压力值设定为990。有没有错误,保持0。
我在做什么错?