我在AVD上运行该程序,当我更改虚拟传感器时,它运行正常,但无法在真实设备上运行。
实际设备仅显示0.0作为压力。
我还在清单文件中添加了以下权限,
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
这是主要的Java类
public class MainActivity extends AppCompatActivity implements SensorEventListener{
public Sensor TemperatureSensor;
public SensorManager sensorManager;
public float pressurereading = 0;
public TextView Textfield;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sensorManager = (SensorManager)getSystemService(getApplicationContext().SENSOR_SERVICE);
TemperatureSensor = sensorManager.getDefaultSensor(Sensor.TYPE_PRESSURE);
Textfield = findViewById(R.id.Textfield);
Textfield.setText(String.valueOf(pressurereading));
}
@Override
public void onResume() {
super.onResume();
sensorManager.registerListener(this,TemperatureSensor,SensorManager
.SENSOR_DELAY_NORMAL);
}
@Override
public void onPause() {
super.onPause();
sensorManager.unregisterListener(this);
}
@Override
public void onAccuracyChanged(Sensor sensor,int Accuracy) {
}
@Override
public void onSensorChanged(SensorEvent e) {
pressurereading = e.values[0];
Textfield.setText(String.valueOf(pressurereading));
}
}
这是xml布局
<RelativeLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Device Temperature"
android:background="@android:drawable/alert_dark_frame"
android:textColorHint="@android:color/holo_green_light"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textSize="30sp"
android:textColor="@android:color/holo_green_light"
android:id="@+id/Textfield"/>
</RelativeLayout>
这是在实际设备上成功构建的Logcat。
06-20 23:13:42.009 8881-8881/? I/art: Late-enabling -Xcheck:jni
06-20 23:13:42.057 8881-8881/? D/TidaProvider: TidaProvider()
06-20 23:13:42.261 8881-8881/lenovo.learning W/System: ClassLoader referenced unknown path: /data/app/lenovo.learning-1/lib/arm64
06-20 23:13:42.275 8881-8881/lenovo.learning I/InstantRun: starting instant run server: is main process
06-20 23:13:42.345 8881-8881/lenovo.learning W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
06-20 23:13:42.361 8881-8881/lenovo.learning D/AccessibilityManager: current package=lenovo.learning, accessibility manager mIsFinalEnabled=false, mOptimizeEnabled=true, mIsUiAutomationEnabled=false, mIsInterestedPackage=false
06-20 23:13:42.443 8881-8881/lenovo.learning E/SensorManager: sensor or listener is null
06-20 23:13:42.488 8881-8910/lenovo.learning I/Adreno: QUALCOMM build : 74bd0df, Ida1be052e0
Build Date : 11/27/17
OpenGL ES Shader Compiler Version: XE031.14.00.04
Local Branch :
Remote Branch :
Remote Branch :
Reconstruct Branch :
06-20 23:13:42.491 8881-8910/lenovo.learning I/Adreno: PFP: 0x005ff087, ME: 0x005ff063
06-20 23:13:42.493 8881-8910/lenovo.learning I/OpenGLRenderer: Initialized EGL, version 1.4
06-20 23:13:42.494 8881-8910/lenovo.learning D/OpenGLRenderer: Swap behavior 1
06-20 23:13:42.525 8881-8881/lenovo.learning W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
答案 0 :(得分:0)
这可能是由于实际设备没有正确的传感器类型引起的。根据SensorManager的文档,getDefaultSensor(int)将返回
与请求的类型相匹配的默认传感器(如果存在),并且应用程序具有必要的权限,否则为为空。
如果您最终将null
传递给sensorManager.registerListener
,则不会触发任何事件,因此将永远不会调用onSensorChanged
。