关于Android studio传感器的问题,如何解决?

想做一个摇一摇的剪刀石头布游戏,功能是:一个手机,第一位同学来摇一次出现石头(剪刀或布),然后第二位同学来摇一摇,出现石头(剪刀或布),然后判断谁输谁赢。大体内容做好了,但是有一个问题,比如第一位同学如果摇晃很久就会出现直接摇到了第二为同学甚至下一轮的第一位同学的现象,想了好久都没有想到怎么解决

MainActivity.class如下,还望指导一下怎么修改

package com.example.hanzhonghao;

import androidx.appcompat.app.AppCompatActivity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.Random;

public class MainActivity extends AppCompatActivity {
    int[] imagesplayer1 = {R.drawable.bu1,R.drawable.jiandao1,R.drawable.shitou1};
    int[] imagesplayer2 = {R.drawable.bu1,R.drawable.jiandao1,R.drawable.shitou1};
    ImageView player;
    TextView textView,textView2;
    SensorManager manager;
    Sensor mAccerometer;
    public static int flag = 0;
    public static int bit;
    public static int bit2;
    private static final int UPDATE_INTERVAL_TIME=100;//两次摇晃之间的间隔
    private static final int SPEED_SHRSHOLD=3000;//摇晃得峰值
    private long lastUpdateTime;//上一次摇晃得时间
    /*手机坐标位置,用于后面检测三个轴的变化,△X,△Y,△Z判断是否达到设定峰值*/
    private float lastX;
    private float lastY;
    private float lastZ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        player = (ImageView)findViewById(R.id.imageView);
        textView = (TextView)findViewById(R.id.textView);
        manager = (SensorManager) getSystemService(SENSOR_SERVICE);
        mAccerometer = manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        textView2 = (TextView) findViewById(R.id.textView3);
    }
    SensorEventListener listener = new SensorEventListener() {
        @Override
        public void onSensorChanged(SensorEvent event) {
            long currentUpdateTime=System.currentTimeMillis();//获取当前时间
            long timeinterval=currentUpdateTime-lastUpdateTime;//计算时间差
            if (timeinterval<UPDATE_INTERVAL_TIME/*时间差是否达到设定间隔*/){
                return;
            }
            lastUpdateTime=currentUpdateTime;//令上一次摇晃的时间等于系统时间
            /*获取出三个轴的坐标*/
            float x=event.values[0];
            float y=event.values[1];
            float z=event.values[2];
            /*获取三个轴的变化值*/
            float deltax=x-lastX;
            float deltay=x-lastY;
            float deltaz=x-lastZ;
            /*得到最终做坐标*/
            lastX=x;
            lastY=y;
            lastZ=z;
            /*判断传感器的峰值*/
            double speed=Math.sqrt(deltax*deltax+deltay*deltay+deltaz*deltaz)/timeinterval*10000;
            if (speed>=SPEED_SHRSHOLD){
                alarm();
            }
        }
        private void alarm() {
            Random r = new Random();
            int player1 = r.nextInt(3);
            int player2 = r.nextInt(3);
            if (flag==0) {
                player.setImageResource(imagesplayer1[player1]);
                if (imagesplayer1[player1] == R.drawable.bu1) {
                    textView2.setText("第一位出布!");
                    textView.setText("");
                    bit=1;
                    flag=1;
                }
                else if (imagesplayer1[player1] == R.drawable.shitou1) {
                    textView2.setText("第一位出石头!");
                    textView.setText("");
                    bit=2;
                    flag=1;
                }
                else if (imagesplayer1[player1] == R.drawable.jiandao1) {
                    textView2.setText("第一位出剪刀!");
                    textView.setText("");
                    bit=3;
                    flag=1;
                }
            }else if (flag==1){
                player.setImageResource(imagesplayer2[player2]);
                if (imagesplayer2[player2] == R.drawable.bu1) {
                    textView2.setText("第二位出布!");
                    bit2=1;
                    if (bit==1){
                        textView.setText("平局!");
                        bit=0;
                        bit2=0;
                    }else if (bit==2){
                        textView.setText("第二位赢!");
                        bit=0;
                        bit2=0;
                    }else if (bit==3){
                        textView.setText("第一位赢!");
                        bit=0;
                        bit2=0;
                    }
                    flag=0;
                }
                else if (imagesplayer2[player2] == R.drawable.shitou1) {
                    textView2.setText("第二位出石头!");
                    bit2=2;
                    if (bit==1){
                        textView.setText("第一位赢!");
                        bit=0;
                        bit2=0;
                    }else if (bit==2){
                        textView.setText("平局!");
                        bit=0;
                        bit2=0;
                    }else if (bit==3){
                        textView.setText("第二位赢!");
                        bit=0;
                        bit2=0;
                    }
                    flag=0;
                }
                else if (imagesplayer2[player2] == R.drawable.jiandao1) {
                    textView2.setText("第二位出剪刀!");
                    bit2=3;
                    if (bit==1){
                        textView.setText("第一位赢!");
                        bit=0;
                        bit2=0;
                    }else if (bit==2){
                        textView.setText("第二位赢!");
                        bit=0;
                        bit2=0;
                    }else if (bit==3){
                        textView.setText("平局!");
                        bit=0;
                        bit2=0;
                    }
                    flag=0;
                }
            }
        }

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

        }
    };

    @Override
    protected void onResume(){
        super.onResume();
        Log.i("test","into resume");
        manager.registerListener(listener,mAccerometer,SensorManager.SENSOR_DELAY_NORMAL);
    }
    @Override
    protected void onStop(){
        manager.unregisterListener(listener);
        super.onStop();
    }
}


activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="猜拳游戏"
        android:textSize="30dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.064" />

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="315dp"
        android:layout_height="210dp"
        android:layout_marginTop="140dp"
        android:orientation="horizontal"
        app:layout_constraintEnd_toEndOf="@+id/textView"
        app:layout_constraintStart_toStartOf="@+id/textView"
        app:layout_constraintTop_toBottomOf="@+id/textView">

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            app:srcCompat="@drawable/kaisi" />
    </LinearLayout>

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        app:layout_constraintEnd_toEndOf="@+id/linearLayout2"
        app:layout_constraintStart_toStartOf="@+id/linearLayout2"
        app:layout_constraintTop_toBottomOf="@+id/linearLayout2" />


</androidx.constraintlayout.widget.ConstraintLayout>

1.摇晃接收到后继续检测,如果是持续摇晃,认为摇晃没有结束,直至摇晃结束

第二位开始摇晃,符合条件引发事件,和1一样检测即可

你可以做一个检测,比如摇晃结束,停顿一会,延迟一段时间,1秒或者两秒的,之后下一次摇晃才算第二次摇一摇开始,这样就避免了你的那个问题