我希望每X秒执行一次代码,但handler.postDelayed无效

时间:2018-05-17 19:16:59

标签: java android android-sensors

好的,所以我想每隔X秒将加速度计值注册到我的数据库。他采取了第一次延迟,但忽略了第二个处理程序。邮件延迟,是否有人知道它可以是什么?我已经尝试过使用计时器,但是这也没有用,所以我在这里找不到选择......

以下是代码:

public class MainActivity extends AppCompatActivity implements SensorEventListener{

private Button receiveButton;
private ListView listView;

private DatabaseReference database;

private ArrayAdapter<String> adapter;
private List<String> items;

private SensorManager sensorManager;
private Sensor senAccelerometer;

private Handler handler = new Handler();
private final int delay = 5000; //in milliseconds

private Accelerometer accelerometer;

private float xValue;
private float yValue;
private float zValue;

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

    sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    senAccelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    sensorManager.registerListener(this, senAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);

    receiveButton = findViewById(R.id.receiveButton);
    listView = findViewById(R.id.listView);

    database = FirebaseDatabase.getInstance().getReference("Accelerometer");

    items = new ArrayList<>();


    items.add("X Value: " + Float.toString(xValue));
    items.add("Y Value: " + Float.toString(yValue));
    items.add("Z Value: " + Float.toString(zValue));
    updateUI();

    receiveButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            database.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    Accelerometer accelerometer = dataSnapshot.getValue(Accelerometer.class);
                    xValue = accelerometer.xValue;
                    yValue = accelerometer.yValue;
                    zValue = accelerometer.zValue;
                    for (Iterator<String> iterator = items.iterator(); iterator.hasNext();){
                        iterator.next();
                        iterator.remove();
                    }
                    items.add("X Value: " + Float.toString(xValue));
                    items.add("Y Value: " + Float.toString(yValue));
                    items.add("Z Value: " + Float.toString(zValue));
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });
            updateUI();
        }
    });
}

private void updateUI(){
    if(adapter == null){
        adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, items);
        listView.setAdapter(adapter);

    } else{
        adapter.notifyDataSetChanged();
    }
}

@Override
public void onSensorChanged(final SensorEvent sensorEvent) {
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            if(sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
                accelerometer = new Accelerometer(sensorEvent.values[0], sensorEvent.values[1], sensorEvent.values[2]);
            }
            database.setValue(accelerometer);
            handler.postDelayed(this, delay);
        }
    }, delay);
}

@Override
public void onAccuracyChanged(Sensor sensor, int i) {

}
}

提前致谢!

3 个答案:

答案 0 :(得分:0)

你应该使用这种模式:

Handler handler = new Handler()
Runnable yourRunnable = new Runnable()
    {

        @Override
        public void run() {
            doYourAction();
            handler.postDelayed(yourRunnable, UPDATE_INTERVAL);
        }
    };

答案 1 :(得分:0)

您可以尝试使用AsyncTask进行递归调用。类似的东西:

public void checkAccelerometer() {
    final Handler handler = new Handler();
    new AsyncTask<Void, Void, Void>() {
        @Override
        protected void onPreExecute() {
            //Update UI if you want/need to
            super.onPreExecute();
        }

        @Override
        protected Void doInBackground(Void... voids) {
            //Do your thing
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    checkAccelerometer();
                }
            }, 5000);
            super.onPostExecute(aVoid);
        }
    }.execute();
}

这将在后台线程中执行您的代码,并在5秒后自行调用以重复继续。如果你想要一种方法来停止这个循环,你可以把所有东西都放在开头的if语句中,并检查你可以在其他地方控制的布尔值。

答案 2 :(得分:0)

我已经解决了,我不得不把它放在oncreate方法中。因为如果将它放在onSensorChanged中,它将在每次数据更改时执行。因此,如果您执行onSensorChanged之外的数据,它将只执行一次,这将确保它不会每0.5毫秒无限更新。