java - android需要比较onSensorChanged方法的当前和之前的event.values

标签 java android

我需要发送带有传感器值变化的 post 请求。它发送了太多请求,因为随着时间戳(纳秒)的每次更改都会调用该方法。我只需要在更改传感器值时发送请求。 我想比较当前的 event.value 和之前的 event.value

import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;

public class MainActivity extends Activity implements SensorEventListener{
    TextView metrics,post;
    private SensorManager sensorManager;
    private float timestamp;
    RequestQueue queue;
    //private Sensor sensor;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        metrics = (TextView) findViewById(R.id.metrics);
        post = (TextView) findViewById(R.id.post);

        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        //sensor = sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);

    }

    protected void onResume()
    {
        super.onResume();
        sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),SensorManager.SENSOR_DELAY_FASTEST);
    }

    protected void onStop()
    {
        sensorManager.unregisterListener(this);
        super.onStop();
    }

    public void onAccuracyChanged(Sensor arg0, int arg1)
    {
        //Do nothing.
    }

    public void onSensorChanged(SensorEvent event)
    {
        if (event.accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE)
        {
            return;
        }

        metrics.setText("Orientation X (Roll) :"+ Float.toString(event.values[0]) +"\n"+
                "Orientation Y (Pitch) :"+ Float.toString(event.values[1]) +"\n"+
                "Orientation Z (Yaw) :"+ Float.toString(event.values[2]));

        if ( event.values[0] != || event.values[1] != || event.values[2] != ) {

            RequestQueue queue = MySingleton.getInstance(this.getApplicationContext()).
                    getRequestQueue();

            String url ="http://10.46.2.179:8080/?X=" + event.values[0] + "&&Y=" + event.values[1] + "&&Z=" + event.values[2];

            StringRequest stringRequest = new StringRequest(com.android.volley.Request.Method.POST, url, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    //post.setText(response);
                    //Log.i("VOLLEY", response);
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    //post.setText(error.toString());
                    //Log.e("VOLLEY", error.toString());
                }
            });

            MySingleton.getInstance(this).addToRequestQueue(stringRequest);
        }

    }
}

最佳答案

考虑使用 Sensor Batching ,越来越多的智能手机支持它。基本上,事件存储在硬件队列中,并定期一次全部发送到您的应用程序。在那里,您可以在一次调用中比较所有结果。

代码中唯一需要更改的行是:

sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),SensorManager.SENSOR_DELAY_FASTEST);

添加您定义的第四个参数:

sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),SensorManager.SENSOR_DELAY_FASTEST, max_report_latency_ns);

这个 max_report_value 在哪里:

When max_report_latency_ns>0, sensor events do not need to be reported as soon as they are detected. They can be temporarily stored in the hardware FIFO and reported in batches, as long as no event is delayed by more than max_report_latency_ns nanoseconds. That is, all events since the previous batch are recorded and returned at once. This reduces the amount of interrupts sent to the SoC and allows the SoC to switch to a lower power mode (idle) while the sensor is capturing and batching data.

关于java - android需要比较onSensorChanged方法的当前和之前的event.values,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44238957/

相关文章:

android - 如何获取自定义 ListView 中每个项目的引用或 ID?

android - 防止用户更改 GPS 状态 Fused Location API

java - 将 Web 应用程序升级到 Spring Boot 2.4 后的 IllegalStateException

java - 有没有一种方法可以更优雅地将数组排序为偶数和奇数?

java - 组件必须有一个有效的对等体 - BufferStrategy

java - 在 Java Swing 中显示 Windows 资源管理器类型的文件结构的最简单方法是什么?

android - Galaxy Nexus 上的奇怪 EOFException

java - 在 Android 中发送和接收 json 对象

java - 圣诞树的错误展示

java - 如何在滑动标签上方添加图像?