该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
package com.example.fanxian;
import android.app.Activity;
import android.content.Context;
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.TextView;
public class Main extends Activity {
private SensorManager sensorManager;
private TextView textView;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.one_layout);
textView=(TextView)findViewById(R.id.button_11);
sensorManager=(SensorManager)getSystemService(Context.SENSOR_SERVICE);
Sensor sensorsuSensor=sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);//加速度
Sensor sensordiSensor=sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);//磁力
sensorManager.registerListener(listener, sensorsuSensor,SensorManager.SENSOR_DELAY_GAME); //监控加速度
sensorManager.registerListener(listener, sensordiSensor,SensorManager.SENSOR_DELAY_GAME);//监控磁力
}
protected void onDestroy() {
if(sensorManager!=null){
sensorManager.unregisterListener(listener);
}
}
private SensorEventListener listener=new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent arg0) {
float[] jiasudu=new float[3];
float[] cili=new float[3];
if(arg0.sensor.getType()==Sensor.TYPE_ACCELEROMETER){
jiasudu=arg0.values.clone();
}else if(arg0.sensor.getType()==Sensor.TYPE_MAGNETIC_FIELD){
cili=arg0.values.clone();
}
float[] R=new float[9];
float[] values1=new float[3];
SensorManager.getRotationMatrix(R, null,jiasudu,cili);
//SensorManager.getRotationMatrix(R, null, cili, jiasudu);
SensorManager.getOrientation(R, values1);
textView.setText(Math.toDegrees(values1[0])+"");
}
@Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
// TODO Auto-generated method stub
}
};
}