我正在为Android移动设备开发一个可扩展的感应和数据处理框架。它将使得各种数据信号(例如温度、电池、压力、Wi-Fi信号强度等)可以通过Android移动设备访问。
为了测试传感器,我在我的Android设备上部署了自己编写的android代码。然而,这里有个限制——我的Android设备只有一组有限的传感器(例如,它没有温度传感器),因此我不能为所有类型的传感器(例如温度、压力等)测试所有编写好的代码。
我在网上查找了SensorSimulator项目(),但它对我无效,我得到了以下错误信息。
我已经编写了以下代码,建议在网站上通过Java项目连接SensorSimulator。 AndroidManifest文件包含以下内容:
为了测试传感器,我在我的Android设备上部署了自己编写的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项目连接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;
}
}
Caused by: android.os.NetworkOnMainThreadException
尝试将其包装在AsyncTask
中? - edthethird