Android Sensör
Android Sensör
Android Sensör
sm = (SensorManager) getSystemService(SENSOR_SERVICE);
accerelerometer =
sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sm.registerListener(this,
accerelerometer,SensorManager.SENSOR_DELAY_NORMAL);
public class SensorActivity extends Activity, implements SensorEventListener {
private final SensorManager mSensorManager;
private final Sensor mAccelerometer;
public SensorActivity() {
mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
}
protected void onResume() {
super.onResume();
mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
}
protected void onPause() {
super.onPause();
mSensorManager.unregisterListener(this);
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
public void onSensorChanged(SensorEvent event) {
}
}
Örnek Uygulama:
package com.example.xxx.lab;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
SensorManager mSemsormanager;
Sensor mAccelerometre;
TextView txtx;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtx=(TextView)findViewById(R.id.sensorx);
mSemsormanager=(SensorManager)getSystemService(SENSOR_SERVICE);
mAccelerometre=mSemsormanager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mSemsormanager.registerListener(this,mAccelerometre,SensorManager.SENSOR_DELAY_NORMAL);
@Override
public void onSensorChanged(SensorEvent event) {
txtx.setText("X :"+ event.values[0]);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}