大家早上好!
我有以下问题:我有传感器加速度计并添加了一个按钮,可以帮助我输入另一个意图,但不知道为什么这个按钮不起作用!!!有什么建议吗?谢谢你的帮助!!
程序是这样的:
package com.dani;
import android.app.Activity;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class FM extends Activity implements SensorEventListener,OnClickListener{
private static final double nbElements = 30;
private SensorManager sensorManager;
private Sensor accelerometer;
Button boton1;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fm);
boton1 = (Button) findViewById(R.id.button1);
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
boolean accelSupported = sensorManager.registerListener(this,
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_FASTEST);
accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
if (!accelSupported) {
sensorManager.unregisterListener(this, accelerometer);
((TextView) findViewById(R.id.textView1)).setText("Accelerometer not
detected");
}
}
@Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
// TODO Auto-generated method stub
}
long now = 0;
long time = 0;
int temp = 0;
public void onSensorChanged(SensorEvent event) {
long tS;
// only if the event is from the accelerometer
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
tS = event.timestamp;
if (now != 0) {
temp++;
if (temp == nbElements) {
time = tS - now;
((TextView) findViewById(R.id.textView1)).setText("Frecuencia : " +
(nbElements * 1000000000 / time)
+ " Hz");
temp = 0;
}
}
// To set up now on the first event and do not change it while we do not have
"nbElements" events
if (temp == 0) {
now = tS;
}
}
boton1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity( new Intent(FM.this,PruebaActivity.class));
}
});
}
public void onClickQuit(View v) {
finish();
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
`