Originariamente inviato da
bisemanu
ciao, grazie mille per l'aiuto, volevo chiederti inoltre come posso accendere lo schermo dalla mia applicazione usando l'accellerometro
Secondo me č meglio se ci pensi un po tu prima.. hai tutto quello che ti serve
Prova ad inserire nel metodo onSensorChanged nella maniera opportuna il codice che ti ho indicato sopra..
Tieni conto pero che i valori degli assi x,y,z dell'accelerometro cambiano di continuo..anche se il telefono č fermo.. ti posto un esempio in cui vedi come variano cosi ti fai un idea se non l hai gią.
codice:
import java.text.DecimalFormat;
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;
public class AccellerometroTestActivity extends Activity implements SensorEventListener{
private SensorManager sensorManager;
double ax,ay,az; // these are the acceleration in x,y and z axis
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sensorManager=(SensorManager) getSystemService(SENSOR_SERVICE);
sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
}
public void onAccuracyChanged(Sensor arg0, int arg1) {
}
public void onSensorChanged(SensorEvent event) {
TextView x = (TextView)this.findViewById(R.id.asse_x);
TextView y = (TextView)this.findViewById(R.id.asse_y);
TextView z = (TextView)this.findViewById(R.id.asse_z);
if (event.sensor.getType()==Sensor.TYPE_ACCELEROMETER){
ax=event.values[0];
ay=event.values[1];
az=event.values[2];
}
/*DecimalFormat df = new DecimalFormat("#.##"); se vuoi formattare il tipo double con solo 2 cifre dopo la virgola
String strx = String.valueOf(df.format(ax));
String stry = String.valueOf(df.format(ay));
String strz = String.valueOf(df.format(az));*/
String strx = String.valueOf(ax);
String stry = String.valueOf(ay);
String strz = String.valueOf(az);
x.setText(strx);
y.setText(stry);
z.setText(strz);
}
}
Buon divertimento