本文整理汇总了Java中android.hardware.SensorEventListener类的典型用法代码示例。如果您正苦于以下问题:Java SensorEventListener类的具体用法?Java SensorEventListener怎么用?Java SensorEventListener使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
SensorEventListener类属于android.hardware包,在下文中一共展示了SensorEventListener类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: onCreate
import android.hardware.SensorEventListener; //导入依赖的package包/类
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
audioPlayer = new AudioPlayer();
audioPlayer.init(this, R.raw.ska_trek);
sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
sensorEventListener = new SensorEventListener() {
@Override
public void onSensorChanged(final SensorEvent event) {
Log.e(TAG, "" + event.timestamp);
xValue.onNext(event.values[0]);
yValue.onNext(event.values[1]);
zValue.onNext(event.values[2]);
}
@Override
public void onAccuracyChanged(final Sensor sensor, final int accuracy) {
//No op
}
};
}
示例2: initializeSensor
import android.hardware.SensorEventListener; //导入依赖的package包/类
public void initializeSensor(Context context){
SensorManager sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
magneticValues = new float[3];
accelerationValues = new float[3];
orientationValues = new float[3];
rotationMatrix = new float[9];
final SensorEventListener mEventListener = new SensorEventListener() {
public void onAccuracyChanged(android.hardware.Sensor sensor, int accuracy) {}
public void onSensorChanged(SensorEvent event) {
switch (event.sensor.getType()) {
case android.hardware.Sensor.TYPE_ACCELEROMETER:
System.arraycopy(event.values, 0, accelerationValues, 0, 3);
break;
case android.hardware.Sensor.TYPE_MAGNETIC_FIELD:
System.arraycopy(event.values, 0, magneticValues, 0, 3);
break;
}
}
};
setListeners(sensorManager, mEventListener);
}
示例3: initializeSensorEventListenerFor
import android.hardware.SensorEventListener; //导入依赖的package包/类
private SensorEventListener initializeSensorEventListenerFor(final FlowableEmitter<SensorRecord> subscriber) {
return new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
subscriber.onNext(createSensorRecordFrom(event));
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {}
};
}
示例4: start
import android.hardware.SensorEventListener; //导入依赖的package包/类
@ProtoMethod(description = "Start the light sensor. Returns the intensity. The value per device might vary", example = "")
@ProtoMethodParam(params = {"function(intensity)"})
public void start() {
super.start();
mListener = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
ReturnObject r = new ReturnObject();
r.put("intensity", event.values[0]);
mCallbackLightChange.event(r);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
switch (accuracy) {
case SensorManager.SENSOR_STATUS_UNRELIABLE:
break;
case SensorManager.SENSOR_STATUS_ACCURACY_LOW:
break;
case SensorManager.SENSOR_STATUS_ACCURACY_MEDIUM:
break;
case SensorManager.SENSOR_STATUS_ACCURACY_HIGH:
break;
}
}
};
isEnabled = mSensormanager.registerListener(mListener, sensor, speed);
}
示例5: BeeSensorProcessor
import android.hardware.SensorEventListener; //导入依赖的package包/类
@SuppressLint("InlinedApi")
public BeeSensorProcessor(Context context, SensorEventListener listener) {
sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
// checking for sensors
if (sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER) == null) {
accelerometerEnabled = false;
}
if (sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE) == null) {
gyroscopeEnabled = false;
}
if (accelerometerEnabled && gyroscopeEnabled) {
sensorRotation = sensorManager.getDefaultSensor(Sensor.TYPE_GAME_ROTATION_VECTOR);
if (sensorRotation == null) {
versionCompatible = false;
}
if (versionCompatible) {
sensorEnabled = true;
sensorManager.registerListener(listener, sensorRotation, 0);
quaternionFromEuler(diffQuaternion, new float[] { 0, 90, 180 });
this.listener = listener;
}
}
}
示例6: KalmanProcessor
import android.hardware.SensorEventListener; //导入依赖的package包/类
public KalmanProcessor(Context context, SensorEventListener listener) {
sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
accelerationSensor = sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY);
sensorManager.registerListener(listener, accelerationSensor, 0);
gyroSensor = sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
sensorManager.registerListener(listener, gyroSensor, 0);
if (accelerationSensor != null && gyroSensor != null) {
this.listener = listener;
sensorEnabled = true;
}
gyroOrientation[0] = 0.0f;
gyroOrientation[1] = 0.0f;
gyroOrientation[2] = 0.0f;
gyroMatrix[0] = 1.0f;
gyroMatrix[1] = 0.0f;
gyroMatrix[2] = 0.0f;
gyroMatrix[3] = 0.0f;
gyroMatrix[4] = 1.0f;
gyroMatrix[5] = 0.0f;
gyroMatrix[6] = 0.0f;
gyroMatrix[7] = 0.0f;
gyroMatrix[8] = 1.0f;
}
示例7: setScreenStateReceiver
import android.hardware.SensorEventListener; //导入依赖的package包/类
private BroadcastReceiver setScreenStateReceiver(final SensorEventListener sensorEventListener)
{
return new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
//HANDLES SCREEN ON/OFF ACTIONS
if(intent.getAction().equals(Intent.ACTION_SCREEN_ON))
{
//RE-REGISTERS PROXIMITY SENSOR LISTENER WHEN SCREEN IS ON TO ACCEPT USER INPUT
sensorManager.registerListener(sensorEventListener, proximitySensor, SensorManager.SENSOR_DELAY_NORMAL);
statusBarDown = false;
}
else if(intent.getAction().equals(Intent.ACTION_SCREEN_OFF))
{
//LOCKS THE SCREEN WHEN SCREEN IS OFF AND NOT IN A PHONE CALL
if(audioManager.getMode() != AudioManager.MODE_IN_CALL)
getSystemService(Context.DEVICE_POLICY_SERVICE);
//UNREGISTERS PROXIMITY SENSOR LISTENER WHEN SCREEN IS OFF TO PREVENT USER INPUT
sensorManager.unregisterListener(sensorEventListener, proximitySensor);
}
}
};
}
示例8: onPause
import android.hardware.SensorEventListener; //导入依赖的package包/类
@Override
protected void onPause() {
super.onPause();
Log.d(TAG, "onPause");
final SensorEventListener activity = this;
JoH.runOnUiThreadDelayed(new Runnable() {
@Override
public void run() {
Log.d(TAG, "Unregistering proximity sensor listener");
try {
mSensorManager.unregisterListener(activity);
} catch (Exception e) {
Log.d(TAG, "Error unregistering proximity listener: " + e);
}
}
}, 10000);
}
示例9: beforeHookedMethod
import android.hardware.SensorEventListener; //导入依赖的package包/类
@Override
protected void beforeHookedMethod(XC_MethodHook.MethodHookParam param) throws Throwable {
SensorEventListener listener = (SensorEventListener) XposedHelpers.getObjectField(param.thisObject, "mListener");
int handle = (int) param.args[0];
Object mgr = XposedHelpers.getObjectField(param.thisObject, "mManager");
HashMap<Integer, Sensor> sensors = (HashMap<Integer, Sensor>) XposedHelpers.getObjectField(mgr, "mHandleToSensor");
Sensor s = sensors.get(handle);
if (listener instanceof VirtualSensorListener) {
float[] values = this.mSensorChange.handleListener(s, (VirtualSensorListener) listener, ((float[]) param.args[1]).clone(), (int) param.args[2], (long) param.args[3], XposedMod.ACCELEROMETER_RESOLUTION, XposedMod.MAGNETIC_RESOLUTION);
if (values != null) {
System.arraycopy(values, 0, param.args[1], 0, values.length);
param.args[0] = XposedMod.sensorTypetoHandle.get(((VirtualSensorListener) listener).getSensor().getType());
}// else param.setResult(null);
}
}
示例10: beforeHookedMethod
import android.hardware.SensorEventListener; //导入依赖的package包/类
@Override
protected void beforeHookedMethod(XC_MethodHook.MethodHookParam param) throws Throwable {
SensorEventListener listener = (SensorEventListener) XposedHelpers.getObjectField(param.thisObject, "mListener");
int handle = (int) param.args[0];
Object mgr = XposedHelpers.getObjectField(param.thisObject, "mManager");
SparseArray<Sensor> sensors = (SparseArray<Sensor>) XposedHelpers.getObjectField(mgr, "mHandleToSensor");
Sensor s = sensors.get(handle);
if (listener instanceof VirtualSensorListener) {
float[] values = this.mSensorChange.handleListener(s, (VirtualSensorListener) listener, ((float[]) param.args[1]).clone(), (int) param.args[2], (long) param.args[3], XposedMod.ACCELEROMETER_RESOLUTION, XposedMod.MAGNETIC_RESOLUTION);
if (values != null) {
System.arraycopy(values, 0, param.args[1], 0, values.length);
param.args[0] = XposedMod.sensorTypetoHandle.get(((VirtualSensorListener) listener).getSensor().getType());
}// else param.setResult(null);
}
}
示例11: initialize
import android.hardware.SensorEventListener; //导入依赖的package包/类
@SuppressWarnings("deprecation")
public static void initialize(Context context) {
mSensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
mListener = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
mDirection = event.values[0];
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
};
}
示例12: run
import android.hardware.SensorEventListener; //导入依赖的package包/类
@Override
public void run() {
mSensorEventListener = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
changed = System.currentTimeMillis();
handler.postDelayed(mRunnable1, 300); // ms
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// don't need it
}
};
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_POWER_CONNECTED);
filter.addAction(Intent.ACTION_POWER_DISCONNECTED);
filter.addAction("android.intent.action.HDMI_PLUGGED");
registerReceiver(mReceiver, filter);
register();
}
示例13: testObserveSensor
import android.hardware.SensorEventListener; //导入依赖的package包/类
@Test
public void testObserveSensor() throws Exception {
setFinalStatic(Build.VERSION.class.getField("SDK_INT"), 9);
when(mockSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)).thenReturn(mockSensor);
final int numberOfEventsToSend = 5;
final ArgumentCaptor<SensorEventListener> argumentCaptor = ArgumentCaptor.forClass(SensorEventListener.class);
when(mockSensorManager.registerListener(
argumentCaptor.capture(),
eq(mockSensor),
eq(0)
)).thenAnswer(new Answer<Boolean>() {
@Override
public Boolean answer(InvocationOnMock invocation) throws Throwable {
for (int i = 0; i < numberOfEventsToSend; ++i) {
argumentCaptor.getValue().onSensorChanged(mock(SensorEvent.class));
}
return true;
}
});
rxSensorManager.observeSensor(Sensor.TYPE_ACCELEROMETER, 0)
.test()
.assertValueCount(numberOfEventsToSend);
}
示例14: testObserveSensorAccuracy
import android.hardware.SensorEventListener; //导入依赖的package包/类
@Test
public void testObserveSensorAccuracy() throws Exception {
setFinalStatic(Build.VERSION.class.getField("SDK_INT"), 9);
when(mockSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)).thenReturn(mockSensor);
final int numberOfEventsToSend = 5;
final ArgumentCaptor<SensorEventListener> argumentCaptor = ArgumentCaptor.forClass(SensorEventListener.class);
when(mockSensorManager.registerListener(
argumentCaptor.capture(),
eq(mockSensor),
eq(0)
)).thenAnswer(new Answer<Boolean>() {
@Override
public Boolean answer(InvocationOnMock invocation) throws Throwable {
for (int i = 0; i < numberOfEventsToSend; ++i) {
argumentCaptor.getValue().onAccuracyChanged(mockSensor, SensorManager.SENSOR_STATUS_ACCURACY_HIGH);
}
return true;
}
});
rxSensorManager.observeSensorAccuracy(Sensor.TYPE_ACCELEROMETER, 0)
.test()
.assertValueCount(numberOfEventsToSend);
}
示例15: observeSensorChanged
import android.hardware.SensorEventListener; //导入依赖的package包/类
@NonNull
public static Observable<SensorEvent> observeSensorChanged(final SensorManager sensorManager,
final Sensor sensor, final int samplingPeriodUs) {
return Observable.fromEmitter(new Action1<AsyncEmitter<SensorEvent>>() {
@Override
public void call(final AsyncEmitter<SensorEvent> sensorEventAsyncEmitter) {
final SensorEventListener sensorListener = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
sensorEventAsyncEmitter.onNext(sensorEvent);
}
@Override
public void onAccuracyChanged(Sensor originSensor, int i) {
// do nothing
}
};
sensorEventAsyncEmitter.setCancellation(() -> sensorManager.unregisterListener(sensorListener, sensor));
sensorManager.registerListener(sensorListener, sensor, samplingPeriodUs);
}
}, AsyncEmitter.BackpressureMode.LATEST);
}