我尝试制作一个电平指示器,当手机到达水平位置时(使用加速度计),它会发出更快的蜂鸣声,触摸屏幕后会发出蜂鸣声,如果再次触摸屏幕,蜂鸣声会停止。
因此,我编写了以下代码,而我的问题是,由于添加了“哔”声部分(在onTouch事件中调用了MyRunnable函数),因此我的应用程序在几秒钟后崩溃(它可以正常工作几秒钟,构建后没有任何错误消息)。
我真的不知道可能是什么问题。我被困在这里,需要一些帮助,谢谢!
公共(public)类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
最佳答案
我在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。