在没有监听器的情况下从 Android 传感器获取数据

发布于 2024-09-02 16:34:04 字数 2093 浏览 0 评论 0原文

我有以下代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

神魇的王 2024-09-09 16:34:06

首先,绝不在主应用程序线程中调用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 when startActivityForResult() returns.

Third, sensor events are asynchronous.

So sensor activity waits for some
reason

It cannot start until you get out of the main application thread, which is being tied up by your Thread.sleep() calls.

then when main activity has nothing to
do, sensors are doing their job.

Precisely.

I want to read sensord data directly,
not using listeners, is it possible?

No. Just use the listeners properly. Here are three sample projects showing the use of sensor listeners.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文