Example usage for android.hardware SensorManager SENSOR_ACCELEROMETER

List of usage examples for android.hardware SensorManager SENSOR_ACCELEROMETER

Introduction

In this page you can find the example usage for android.hardware SensorManager SENSOR_ACCELEROMETER.

Prototype

int SENSOR_ACCELEROMETER

To view the source code for android.hardware SensorManager SENSOR_ACCELEROMETER.

Click Source Link

Document

A constant describing an accelerometer.

Usage

From source file:com.intel.xdk.accelerometer.Accelerometer.java

@JavascriptInterface
public void start(int time) {
    mTime = time;/*from  w ww. jav  a2 s.  c o m*/
    if (!started) {
        sensorManager.registerListener(this, SensorManager.SENSOR_ACCELEROMETER,
                SensorManager.SENSOR_DELAY_GAME);
        started = true;
    }
}

From source file:com.intel.xdk.accelerometer.Accelerometer.java

public void onSensorChanged(int sensor, float[] values) {
    if (sensor != SensorManager.SENSOR_ACCELEROMETER || values.length < 3)
        return;/* ww  w. ja  va2s  .c om*/
    long curTime = System.currentTimeMillis();

    if (lastUpdate == -1 || (curTime - lastUpdate) > mTime) {

        lastUpdate = curTime;

        //android reports 0-10 - make it 0-1 for consistency with iPhone
        float x = values[DATA_X] / 10;
        float y = values[DATA_Y] / 10;
        float z = values[DATA_Z] / 10;

        //if in landscape, android swaps x and y axes - swap them back for consistency with iPhone
        if (activity.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
            float oldx = x, oldy = y;
            y = oldx * -1;
            x = oldy;
        }
        //wrapped in try/catch to fix errors after closing can roll in appLab
        String js = "javascript:try{intel.xdk.accelerometer.setAcceleration(new intel.xdk.acceleration.Acceleration("
                + x + "," + y + "," + z + ",false));}catch(e){}";
        //String js = "javascript:new intel.xdk.acceleration.Acceleration("+x+","+y+","+z+",false);";
        //String js = "javascript:console.log('before'); new intel.xdk.acceleration.Acceleration("+x+","+y+","+z+",false);console.log('after'); ";
        //String js = "javascript:alert('0');";
        //would be nice to have a roundtrip feedback loop in order to stop injecting when it is causing errors

        webView.loadUrl(js);
    }
}

From source file:org.openintents.shopping.ui.ShoppingActivity.java

private void registerSensor() {
    if (!mUseSensor) {
        // Don't use sensors
        return;/*from w w w .  jav  a 2  s  .co  m*/
    }

    if (mItemsView.mMode == MODE_IN_SHOP) {
        if (mSensorManager == null) {
            mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        }
        mSensorManager.registerListener(mMySensorListener, SensorManager.SENSOR_ACCELEROMETER,
                SensorManager.SENSOR_DELAY_UI);
    }

}