为什么哔声会让应用程序崩溃?

时间:2018-03-15 09:47:47

标签: android crash handler accelerometer ontouchlistener

我尝试制作一个水平指示器,当手机到达水平位置时(使用加速度计)会发出更快的哔声,触摸屏幕后会发出哔哔声,如果再次触摸屏幕则会停止。 所以我做了以下代码,我遇到的问题是,因为我已经添加了" beep"声音部分(在onTouch事件中调用MyRunnable函数),我的应用程序在几秒钟后崩溃(它可以正常工作几秒钟,并且我在构建后没有错误消息)。 我真的不知道问题可能是什么。我被困在这里需要一些帮助,谢谢!

公共类MainActivity扩展AppCompatActivity实现SensorEventListener,View.OnTouchListener {

Sensor mySensor;
SensorManager SM;

float ANGLEX, ANGLEY, ANGLEZ;
int aX, aY, aZ;
int roundANGLEX, roundANGLEY, roundANGLEZ;
int Xetal, Yetal;
double centre;
int Rcentre;
int Rcentre2;
boolean active;
int test;

int i=0;
private Handler myHandler;

private Runnable myRunnable = new Runnable() {
    @Override
    public void run() {
        // Play a periodic beep which accelerates according to Rcentre2

        ToneGenerator toneGen1 = new ToneGenerator(AudioManager.STREAM_MUSIC, 100);
        toneGen1.startTone(ToneGenerator.TONE_PROP_BEEP,150);
        myHandler.postDelayed(this,(long) Math.sqrt(Rcentre2*20)+50);
    }
};

@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //Créée notre Sensor Manager
    SM = (SensorManager) getSystemService(SENSOR_SERVICE);

    //Accéléromètre
    mySensor = SM.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

    //Registre Sensor Listener
    SM.registerListener(this, mySensor, 1000000, 1000000);


    ((ConstraintLayout) findViewById(R.id.layout_main)).setOnTouchListener((View.OnTouchListener) this);
}



@Override
public void onAccuracyChanged(Sensor sensor, int i) {
    //Pas utilisé
}


@Override
public void onSensorChanged(SensorEvent sensorEvent) {

    float z_stable = ((int) ((sensorEvent.values[2]) * 100)) / 100.0f;


    ANGLEX = (float) (((float) (Math.acos(sensorEvent.values[0] / 9.807)) * (180 / Math.PI))); //I get the accelerometer's values
    ANGLEY = (float) (((float) (Math.acos(sensorEvent.values[1] / 9.807)) * (180 / Math.PI)));
    ANGLEZ = (float) (((float) (Math.acos(z_stable / 9.807)) * (180 / Math.PI)));

    roundANGLEX = Math.round(ANGLEX);
    roundANGLEY = Math.round(ANGLEY);
    roundANGLEZ = Math.round(ANGLEZ);

    aX = roundANGLEX;
    aY = roundANGLEY;
    aZ = roundANGLEZ;


    Xetal = aX - 88; //Xetal and Yetal are 0 when phone is on plane surface
    Yetal = aY - 90; //and go from -90 to +90

    centre = Math.sqrt(Xetal * Xetal + Yetal * Yetal); //gives the "distance" from the "center => the smaller centre gets, the closer the phone approach horizontal
    Rcentre = (int) Math.round(centre);
    Rcentre2 = (int) Math.round(centre * 100);
}


public boolean onTouch(View view, MotionEvent motionEvent) {
    if (active == true) {
        active = false;
        myHandler.removeCallbacks(myRunnable);
    }
    else if (active == false) {
        active = true;

            myHandler = new Handler();
            myHandler.postDelayed(myRunnable,0); 

        }
    return false;
}

}

以下是Logcat的信息,它似乎显示出一些问题,但我不知道这意味着什么。 logcat information

2 个答案:

答案 0 :(得分:0)

我在ToneGenerator crashes in android 6.0

找到了一个对我有用的答案

“它只是释放ToneGenerator的创建对象,因为快速创建'ToneGenerator'对象而不释放它们会导致应用程序崩溃。”

        ToneGenerator toneGen1 = new ToneGenerator(AudioManager.STREAM_MUSIC, 100);
        toneGen1.startTone(ToneGenerator.TONE_PROP_BEEP,150);
        toneGen1.release();

我添加了toneGen1.release();它现在工作正常。

归功于Mahmoud Farahat。

答案 1 :(得分:0)

使用似乎有效的信号量(tonebusy)(KOTLIN):

var tonebusy : Boolean = false

fun beep(i : Int) {

    if (tonebusy) {
        return
    }
    else 
    {
      tonebusy = true
      val toneGen1 = ToneGenerator(AudioManager.STREAM_MUSIC, 100)
      toneGen1.startTone(ToneGenerator.TONE_CDMA_PIP, i)
      toneGen1.release();
    }
    tonebusy = false

}