I have a little application which shows me the coordinates of sensors in LogCat on Eclipse and I want same thing on the screen of the phone. Can you give me some tips how I should do it? It doesn't have to be a code, just tips.
I don't know how to updating TextView in MainActivity by coordinates from mySensors.java . I receive only static "x=0 y=0 z=0". Can't nothing more...
My MainActivity.java:
public class MainActivity extends Activity {
SensorManager mSensorManager;
mySensors mSensors;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSensors = new mySensors();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
mSensorManager.unregisterListener(mSensors);
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
initSensors();
}
private void initSensors() {
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
List<Sensor> accel_sensor_list = mSensorManager
.getSensorList(Sensor.TYPE_ACCELEROMETER);
mSensorManager.registerListener((SensorEventListener) mSensors,
accel_sensor_list.get(0), SensorManager.SENSOR_DELAY_UI);
List<Sensor> magn_sensor_list = mSensorManager
.getSensorList(Sensor.TYPE_MAGNETIC_FIELD);
mSensorManager.registerListener((SensorEventListener) mSensors,
magn_sensor_list.get(0), SensorManager.SENSOR_DELAY_UI);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
My mySensors.java:
public class mySensors implements SensorEventListener {
private static int VECTOR_SIZE=3;
private static int MATRIX_SIZE=16;
int type, heading_deg, pitch_deg, roll_deg;
boolean isReady;
float[] vals = new float[VECTOR_SIZE];
float[] accels = new float[VECTOR_SIZE];
float[] mags = new float[VECTOR_SIZE];
float[] orientation_rad = new float[VECTOR_SIZE];
float[] R_orig = new float[MATRIX_SIZE];
float[] I_orig = new float[MATRIX_SIZE];
float[] R_remapped = new float[MATRIX_SIZE];
public void onSensorChanged(SensorEvent se) {
type = se.sensor.getType();
vals = se.values.clone();
switch (type) {
case Sensor.TYPE_ACCELEROMETER:
accels = vals.clone();
break;
case Sensor.TYPE_MAGNETIC_FIELD:
mags = vals.clone();
isReady = true;
}
if (isReady && mags != null && accels != null) {
isReady = false;
SensorManager.getRotationMatrix(R_orig, I_orig, accels, mags);
// zmiana układ współrzednych odniesienia (!)
// SensorManager.remapCoordinateSystem(R_orig,
// SensorManager.AXIS_Z, SensorManager.AXIS_MINUS_X, R_remapped);
SensorManager.getOrientation(R_orig, orientation_rad);
heading_deg = (int) (orientation_rad[0] * 57.2957795f); // radians
// to
// degrees
pitch_deg = (int) (orientation_rad[1] * 57.2957795f);
roll_deg = (int) (orientation_rad[2] * 57.2957795f);
Log.w("Sensors:",
"x=" + heading_deg +
" y=" + pitch_deg +
" z=" + roll_deg
);
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
}
您可以创建自己的Listener接口( 示例方法 ),以监听onSensorChanged结果,并调用主Activity,在此您可以将Text设置为TextView。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.