在没有监听器的情况下从 Android 传感器获取数据
我有以下代码:
public class readSensorsData extends Activity implements SensorListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sm = (SensorManager) getSystemService(SENSOR_SERVICE);
}
@Override
public void onSensorChanged(int sensor, float[] values) {
synchronized (this) {
if (sensor == SensorManager.SENSOR_ORIENTATION) {
//get orientation
}
if (sensor == SensorManager.SENSOR_ACCELEROMETER) {
//get acceleration
}
}
if (sensor == SensorManager.SENSOR_MAGNETIC_FIELD) {
//get magnetic fiels
}
Bundle bundle = new Bundle(); //results from activity
bundle.putFloatArray("acceleration", array);
Intent mIntent = new Intent();
mIntent.putExtras(bundle);
setResult(RESULT_OK, mIntent);
finish();
}
public void onAccuracyChanged(int sensor, int accuracy) {
}
@Override
protected void onResume() {
super.onResume();
sm.registerListener(this,
SensorManager.SENSOR_ORIENTATION |
SensorManager.SENSOR_ACCELEROMETER |
SensorManager.SENSOR_MAGNETIC_FIELD,
SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onStop() {
sm.unregisterListener(this);
super.onStop();
}
}
代码正在工作,但我的问题是:当我以这种方式从主要活动中调用它时:
Intent i = new Intent(this, readSensorsData.class);
startActivityForResult(i, 1); //1 is for sensors
for(int j=0;j<10;j++)
{
//do sth else here!!!!
try {
Thread.sleep(1000);
} catch (InterruptedException e) {e.printStackTrace();}
//here code for showing result from sensor activity
}
然后我可以看到“做其他事情!!!”的 10 倍结果当循环结束时,我可以看到活动的结果。因此,传感器活动会因某种原因而等待,然后当主要活动无关时,传感器就会开始工作。
当然我已经很好地实现了: onActivityResult(int requestCode, int resultCode, Intent Intent)
我想直接读取传感器数据,而不是使用侦听器,这可能吗?
I have following code:
public class readSensorsData extends Activity implements SensorListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sm = (SensorManager) getSystemService(SENSOR_SERVICE);
}
@Override
public void onSensorChanged(int sensor, float[] values) {
synchronized (this) {
if (sensor == SensorManager.SENSOR_ORIENTATION) {
//get orientation
}
if (sensor == SensorManager.SENSOR_ACCELEROMETER) {
//get acceleration
}
}
if (sensor == SensorManager.SENSOR_MAGNETIC_FIELD) {
//get magnetic fiels
}
Bundle bundle = new Bundle(); //results from activity
bundle.putFloatArray("acceleration", array);
Intent mIntent = new Intent();
mIntent.putExtras(bundle);
setResult(RESULT_OK, mIntent);
finish();
}
public void onAccuracyChanged(int sensor, int accuracy) {
}
@Override
protected void onResume() {
super.onResume();
sm.registerListener(this,
SensorManager.SENSOR_ORIENTATION |
SensorManager.SENSOR_ACCELEROMETER |
SensorManager.SENSOR_MAGNETIC_FIELD,
SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onStop() {
sm.unregisterListener(this);
super.onStop();
}
}
code is working but here goes my problem: when I call it from main activity in this way:
Intent i = new Intent(this, readSensorsData.class);
startActivityForResult(i, 1); //1 is for sensors
for(int j=0;j<10;j++)
{
//do sth else here!!!!
try {
Thread.sleep(1000);
} catch (InterruptedException e) {e.printStackTrace();}
//here code for showing result from sensor activity
}
then i can see 10 times result of 'doing sth else!!!' and when loop is over i can see then result from activity. So sensor activity waits for some reason and then when main activity has nothing to do, sensors are doing their job.
of course I have well implemented: onActivityResult(int requestCode, int resultCode, Intent intent)
I want to read sensord data directly, not using listeners, is it possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,绝不在主应用程序线程中调用
Thread.sleep()
。其次,startActivityForResult() 是异步的。当
startActivityForResult()
返回时,其他活动尚未启动。第三,传感器事件是异步的。
在您退出主应用程序线程之前它无法启动,该线程被您的 Thread.sleep() 调用所占用。
恰恰。
不,只要正确使用听众即可。 这里是三个示例项目,展示了传感器侦听器的使用。
First, never call
Thread.sleep()
in the main application thread.Second,
startActivityForResult()
is asynchronous. The other activity will not have started whenstartActivityForResult()
returns.Third, sensor events are asynchronous.
It cannot start until you get out of the main application thread, which is being tied up by your
Thread.sleep()
calls.Precisely.
No. Just use the listeners properly. Here are three sample projects showing the use of sensor listeners.