當前位置: 首頁>>代碼示例>>Java>>正文


Java SensorEventListener類代碼示例

本文整理匯總了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
        }
    };
}
 
開發者ID:EmmettWilson,項目名稱:ShakeItUpCodelab,代碼行數:25,代碼來源:MainActivity.java

示例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);
}
 
開發者ID:PawelTypiak,項目名稱:Checkerboard-IMU-Comparator,代碼行數:22,代碼來源:SensorInitializer.java

示例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) {}
    };
}
 
開發者ID:ubikgs,項目名稱:AndroidSensors,代碼行數:13,代碼來源:IMUSensorGatherer.java

示例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);
}
 
開發者ID:victordiaz,項目名稱:phonk,代碼行數:33,代碼來源:PLightIntensity.java

示例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;
		}
	}
}
 
開發者ID:caiocomandulli,項目名稱:engine-stereo,代碼行數:25,代碼來源:BeeSensorProcessor.java

示例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;
}
 
開發者ID:caiocomandulli,項目名稱:engine-stereo,代碼行數:27,代碼來源:KalmanProcessor.java

示例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);
			}
		}
	};
}
 
開發者ID:piatt,項目名稱:GestureWave,代碼行數:27,代碼來源:SensorService.java

示例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);
}
 
開發者ID:NightscoutFoundation,項目名稱:xDrip,代碼行數:19,代碼來源:Reminders.java

示例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);
    }
}
 
開發者ID:Frazew,項目名稱:VirtualSensor,代碼行數:17,代碼來源:API24.java

示例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);
    }
}
 
開發者ID:Frazew,項目名稱:VirtualSensor,代碼行數:17,代碼來源:API23.java

示例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) {
        }

    };
}
 
開發者ID:iWay7,項目名稱:BikeHelper,代碼行數:18,代碼來源:DirectionUtils.java

示例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();

}
 
開發者ID:parrotgeek1,項目名稱:ParrotModFloApp,代碼行數:24,代碼來源:MyService.java

示例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);
}
 
開發者ID:bryandunlap,項目名稱:RxSensorManager,代碼行數:24,代碼來源:RxSensorManagerTest.java

示例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);
}
 
開發者ID:bryandunlap,項目名稱:RxSensorManager,代碼行數:24,代碼來源:RxSensorManagerTest.java

示例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);
}
 
開發者ID:ArturVasilov,項目名稱:AndroidSchool,代碼行數:24,代碼來源:RxSensor.java


注:本文中的android.hardware.SensorEventListener類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。