gpt4 book ai didi

java - 用于模拟 Android 传感器的 openintents 的替代方案

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:00:02 26 4
gpt4 key购买 nike

我正在为 Android 移动设备开发一个可扩展的传感和数据处理框架。它将支持通过 Android 移动设备访问各种数据信号(例如,温度、电池、压力、wifi 信号强度等)。

为了测试传感器,我在我的 Android 设备上部署了我编写的 Android 代码。在这里,限制来了---我的安卓设备有一组有限的传感器(例如,它没有温度传感器)所以,我无法测试所有类型传感器(例如,温度、压力等)的所有编写代码。

在互联网上,我检查了SensorSimulator 项目(),但它对我不起作用。我收到以下错误。

05-03 01:40:40.766: E/AndroidRuntime(10139): FATAL EXCEPTION: main
05-03 01:40:40.766: E/AndroidRuntime(10139): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.helloworldsensorsimulator/com.example.helloworldsensorsimulator.MainActivity}: android.os.NetworkOnMainThreadException
05-03 01:40:40.766: E/AndroidRuntime(10139): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
05-03 01:40:40.766: E/AndroidRuntime(10139): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2146)
05-03 01:40:40.766: E/AndroidRuntime(10139): at android.app.ActivityThread.access$700(ActivityThread.java:140)
05-03 01:40:40.766: E/AndroidRuntime(10139): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1238)
05-03 01:40:40.766: E/AndroidRuntime(10139): at android.os.Handler.dispatchMessage(Handler.java:99)
05-03 01:40:40.766: E/AndroidRuntime(10139): at android.os.Looper.loop(Looper.java:177)
05-03 01:40:40.766: E/AndroidRuntime(10139): at android.app.ActivityThread.main(ActivityThread.java:4947)
05-03 01:40:40.766: E/AndroidRuntime(10139): at java.lang.reflect.Method.invokeNative(Native Method)
05-03 01:40:40.766: E/AndroidRuntime(10139): at java.lang.reflect.Method.invoke(Method.java:511)
05-03 01:40:40.766: E/AndroidRuntime(10139): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
05-03 01:40:40.766: E/AndroidRuntime(10139): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
05-03 01:40:40.766: E/AndroidRuntime(10139): at dalvik.system.NativeStart.main(Native Method)
05-03 01:40:40.766: E/AndroidRuntime(10139): Caused by: android.os.NetworkOnMainThreadException
05-03 01:40:40.766: E/AndroidRuntime(10139): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1118)
05-03 01:40:40.766: E/AndroidRuntime(10139): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
05-03 01:40:40.766: E/AndroidRuntime(10139): at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
05-03 01:40:40.766: E/AndroidRuntime(10139): at libcore.io.IoBridge.connect(IoBridge.java:112)
05-03 01:40:40.766: E/AndroidRuntime(10139): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
05-03 01:40:40.766: E/AndroidRuntime(10139): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
05-03 01:40:40.766: E/AndroidRuntime(10139): at java.net.Socket.startupSocket(Socket.java:566)
05-03 01:40:40.766: E/AndroidRuntime(10139): at java.net.Socket.tryAllAddresses(Socket.java:127)
05-03 01:40:40.766: E/AndroidRuntime(10139): at java.net.Socket.<init>(Socket.java:177)
05-03 01:40:40.766: E/AndroidRuntime(10139): at java.net.Socket.<init>(Socket.java:149)
05-03 01:40:40.766: E/AndroidRuntime(10139): at org.openintents.sensorsimulator.hardware.SensorSimulatorClient.connect(SensorSimulatorClient.java:116)
05-03 01:40:40.766: E/AndroidRuntime(10139): at org.openintents.sensorsimulator.hardware.SensorManagerSimulator.connectSimulator(SensorManagerSimulator.java:220)
05-03 01:40:40.766: E/AndroidRuntime(10139): at com.example.helloworldsensorsimulator.MainActivity.onCreate(MainActivity.java:37)
05-03 01:40:40.766: E/AndroidRuntime(10139): at android.app.Activity.performCreate(Activity.java:5207)
05-03 01:40:40.766: E/AndroidRuntime(10139): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
05-03 01:40:40.766: E/AndroidRuntime(10139): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2085)
05-03 01:40:40.766: E/AndroidRuntime(10139): ... 11 more

我写了如下代码,网站上建议通过Java Project连接SensorSimulator。 AndroidManifest 文件包含

<uses-permission android:name="android.permission.INTERNET"/>


import org.openintents.sensorsimulator.hardware.Sensor;
import org.openintents.sensorsimulator.hardware.SensorEvent;
import org.openintents.sensorsimulator.hardware.SensorEventListener;
import org.openintents.sensorsimulator.hardware.SensorManagerSimulator;

public class MainActivity extends Activity {

private SensorManagerSimulator mSensorManager;

private SensorEventListener mEventListenerAccelerometer;
private SensorEventListener mEventListenerGravity;
private SensorEventListener mEventListenerLinearAcceleration;
private SensorEventListener mEventListenerLight;
private SensorEventListener mEventListenerTemperature;
private SensorEventListener mEventListenerOrientation;
private SensorEventListener mEventListenerMagneticField;
private SensorEventListener mEventListenerPressure;
private SensorEventListener mEventListenerRotationVector;
private SensorEventListener mEventListenerBarcode;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mSensorManager = SensorManagerSimulator.getSystemService(this,
SENSOR_SERVICE);

mSensorManager.connectSimulator();


initListeners();
}

private void initListeners() {
mEventListenerAccelerometer = new SensorEventListener() {

@Override
public void onSensorChanged(SensorEvent event) {
float[] values = event.values;
System.out.println("Accelerometer: " + values[0]
+ ", " + values[1] + ", " + values[2]);
/*mTextViewAccelerometer.setText("Accelerometer: " + values[0]
+ ", " + values[1] + ", " + values[2]);*/
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
};
mEventListenerLinearAcceleration = new SensorEventListener() {

@Override
public void onSensorChanged(SensorEvent event) {
float[] values = event.values;
System.out.println("Linear Acceleration: "
+ values[0] + ", " + values[1] + ", " + values[2]);
/*mTextViewLinearAcceleration.setText("Linear Acceleration: "
+ values[0] + ", " + values[1] + ", " + values[2]);*/
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
};
mEventListenerGravity = new SensorEventListener() {

@Override
public void onSensorChanged(SensorEvent event) {
float[] values = event.values;
System.out.println("Gravity: " + values[0] + ", "
+ values[1] + ", " + values[2]);
/*mTextViewGravity.setText("Gravity: " + values[0] + ", "
+ values[1] + ", " + values[2]);*/
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
};
mEventListenerMagneticField = new SensorEventListener() {

@Override
public void onSensorChanged(SensorEvent event) {
float[] values = event.values;

System.out.println("Compass: " + values[0] + ", "
+ values[1] + ", " + values[2]);
/*mTextViewMagneticField.setText("Compass: " + values[0] + ", "
+ values[1] + ", " + values[2]);
*/ }

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
};
mEventListenerOrientation = new SensorEventListener() {

@Override
public void onSensorChanged(SensorEvent event) {
float[] values = event.values;
System.out.println("Orientation: " + values[0] + ", "
+ values[1] + ", " + values[2]);
/*mTextViewOrientation.setText("Orientation: " + values[0] + ", "
+ values[1] + ", " + values[2]);*/
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
};
mEventListenerTemperature = new SensorEventListener() {

@Override
public void onSensorChanged(SensorEvent event) {
float[] values = event.values;
System.out.println("Temperature: " + values[0]);
//mTextViewTemperature.setText("Temperature: " + values[0]);
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
};
mEventListenerLight = new SensorEventListener() {

@Override
public void onSensorChanged(SensorEvent event) {
float[] values = event.values;
System.out.println("Light: " + values[0]);
//mTextViewLight.setText("Light: " + values[0]);
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
};
mEventListenerPressure = new SensorEventListener() {

@Override
public void onSensorChanged(SensorEvent event) {
float[] values = event.values;
System.out.println("Pressure: " + values[0]);
//mTextViewPressure.setText("Pressure: " + values[0]);
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
};
mEventListenerRotationVector = new SensorEventListener() {

@Override
public void onSensorChanged(SensorEvent event) {
float[] values = event.values;
System.out.println("RotationVector: " + values[0]
+ ", " + values[1] + ", " + values[2]);
/* mTextViewRotationVector.setText("RotationVector: " + values[0]
+ ", " + values[1] + ", " + values[2]);*/
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
};

mEventListenerBarcode = new SensorEventListener() {

@Override
public void onSensorChanged(SensorEvent event) {
System.out.println("Barcode: " + event.barcode);
//mTextViewBarcode.setText("Barcode: " + event.barcode);
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
};
}

@Override
protected void onResume() {
super.onResume();
/* mSensorManager.registerListener(mEventListenerAccelerometer,
mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_FASTEST);
mSensorManager.registerListener(mEventListenerLinearAcceleration,
mSensorManager
.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION),
SensorManager.SENSOR_DELAY_FASTEST);
mSensorManager.registerListener(mEventListenerGravity,
mSensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY),
SensorManager.SENSOR_DELAY_FASTEST);
mSensorManager.registerListener(mEventListenerMagneticField,
mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
SensorManager.SENSOR_DELAY_FASTEST);
mSensorManager.registerListener(mEventListenerOrientation,
mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
SensorManager.SENSOR_DELAY_FASTEST);*/
mSensorManager.registerListener(mEventListenerTemperature,
mSensorManager.getDefaultSensor(Sensor.TYPE_TEMPERATURE),
SensorManager.SENSOR_DELAY_FASTEST);
/*mSensorManager.registerListener(mEventListenerLight,
mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT),
SensorManager.SENSOR_DELAY_FASTEST);
mSensorManager.registerListener(mEventListenerPressure,
mSensorManager.getDefaultSensor(Sensor.TYPE_PRESSURE),
SensorManager.SENSOR_DELAY_FASTEST);
mSensorManager.registerListener(mEventListenerBarcode,
mSensorManager.getDefaultSensor(Sensor.TYPE_BARCODE_READER),
SensorManager.SENSOR_DELAY_FASTEST);
mSensorManager.registerListener(mEventListenerRotationVector,
mSensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR),
SensorManager.SENSOR_DELAY_FASTEST);*/
}

@Override
protected void onStop() {
/*mSensorManager.unregisterListener(mEventListenerAccelerometer);
mSensorManager.unregisterListener(mEventListenerLinearAcceleration);
mSensorManager.unregisterListener(mEventListenerGravity);
mSensorManager.unregisterListener(mEventListenerMagneticField);
mSensorManager.unregisterListener(mEventListenerOrientation);*/
mSensorManager.unregisterListener(mEventListenerTemperature);
/*mSensorManager.unregisterListener(mEventListenerLight);
mSensorManager.unregisterListener(mEventListenerPressure);
mSensorManager.unregisterListener(mEventListenerRotationVector);
mSensorManager.unregisterListener(mEventListenerBarcode);*/
super.onStop();
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

最佳答案

调用 SensorManager.connectSimulator() 构成网络操作,如您的堆栈跟踪所示。因此,您正试图在应用程序的主线程上执行网络操作。默认情况下,Android 不允许这样做,因此您有两个选择。

  1. 使用另一个线程或async task . AsyncTask 语法和使用约定可能有点困惑,所以这里有一个例子:AsyncTask Android example .
  2. Turn off strict mode (不推荐)

关于java - 用于模拟 Android 传感器的 openintents 的替代方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22423989/

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