Example usage for android.hardware SensorManager getDefaultSensor

List of usage examples for android.hardware SensorManager getDefaultSensor

Introduction

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

Prototype

public Sensor getDefaultSensor(int type) 

Source Link

Document

Use this method to get the default sensor for a given type.

Usage

From source file:com.zainsoft.ramzantimetable.QiblaActivity.java

private void unregisterListeners() {
    ((LocationManager) getSystemService(Context.LOCATION_SERVICE)).removeUpdates(qiblaManager);

    ((LocationManager) getSystemService(Context.LOCATION_SERVICE)).removeUpdates(qiblaManager);
    SensorManager mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    Sensor gsensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    Sensor msensor = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
    mSensorManager.unregisterListener(qiblaManager, gsensor);
    mSensorManager.unregisterListener(qiblaManager, msensor);
    cancelSchedule();//from   w  w  w  .  j  a v a2s.  c o m

}

From source file:com.zainsoft.ramzantimetable.QiblaActivity.java

private void registerListeners() {
    SharedPreferences perfs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

    SensorManager mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    Sensor gsensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    Sensor msensor = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
    mSensorManager.registerListener(qiblaManager, gsensor, SensorManager.SENSOR_DELAY_GAME);
    mSensorManager.registerListener(qiblaManager, msensor, SensorManager.SENSOR_DELAY_GAME);
    schedule();//w  w w. java  2 s .co  m
    isRegistered = true;

}

From source file:io.github.msc42.masterthemaze.GameActivity.java

private boolean checkIfSensorForMotionControlIsAvailableAndInitMotionControlParameter() {
    if (mMotion) {
        SensorManager sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        Sensor accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

        if (accelerometer == null) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle(R.string.error).setMessage(R.string.noAccelerometerExisting).setCancelable(false)
                    .setPositiveButton(R.string.okay, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            finish();//w ww  .ja  va  2 s .  co  m
                        }
                    });

            builder.create().show();
            return false;
        }

        mSensitivity = mIntent.getFloatExtra(Constants.EXTRA_MESSAGE_SENSITIVITY,
                Constants.DEFAULT_SENSOR_SENSITIVITY);
        mSpeed = mIntent.getIntExtra(Constants.EXTRA_MESSAGE_SPEED, Constants.DEFAULT_SPEED);

        mRotation = getWindowManager().getDefaultDisplay().getRotation();
    }

    return true;
}

From source file:com.example.android.batchstepsensor.BatchStepSensorFragment.java

/**
 * Register a {@link android.hardware.SensorEventListener} for the sensor and max batch delay.
 * The maximum batch delay specifies the maximum duration in microseconds for which subsequent
 * sensor events can be temporarily stored by the sensor before they are delivered to the
 * registered SensorEventListener. A larger delay allows the system to handle sensor events more
 * efficiently, allowing the system to switch to a lower power state while the sensor is
 * capturing events. Once the max delay is reached, all stored events are delivered to the
 * registered listener. Note that this value only specifies the maximum delay, the listener may
 * receive events quicker. A delay of 0 disables batch mode and registers the listener in
 * continuous mode.//from  www .j a v  a 2s.  c o  m
 * The optimium batch delay depends on the application. For example, a delay of 5 seconds or
 * higher may be appropriate for an  application that does not update the UI in real time.
 *
 * @param maxdelay
 * @param sensorType
 */
private void registerEventListener(int maxdelay, int sensorType) {
    // BEGIN_INCLUDE(register)

    // Keep track of state so that the correct sensor type and batch delay can be set up when
    // the app is restored (for example on screen rotation).
    mMaxDelay = maxdelay;
    if (sensorType == Sensor.TYPE_STEP_COUNTER) {
        mState = STATE_COUNTER;
        /*
        Reset the initial step counter value, the first event received by the event listener is
        stored in mCounterSteps and used to calculate the total number of steps taken.
         */
        mCounterSteps = 0;
        Log.i(TAG, "Event listener for step counter sensor registered with a max delay of " + mMaxDelay);
    } else {
        mState = STATE_DETECTOR;
        Log.i(TAG, "Event listener for step detector sensor registered with a max delay of " + mMaxDelay);
    }

    // Get the default sensor for the sensor type from the SenorManager
    SensorManager sensorManager = (SensorManager) getActivity().getSystemService(Activity.SENSOR_SERVICE);
    // sensorType is either Sensor.TYPE_STEP_COUNTER or Sensor.TYPE_STEP_DETECTOR
    Sensor sensor = sensorManager.getDefaultSensor(sensorType);

    // Register the listener for this sensor in batch mode.
    // If the max delay is 0, events will be delivered in continuous mode without batching.
    final boolean batchMode = sensorManager.registerListener(mListener, sensor,
            SensorManager.SENSOR_DELAY_NORMAL, maxdelay);

    if (!batchMode) {
        // Batch mode could not be enabled, show a warning message and switch to continuous mode
        getCardStream().getCard(CARD_NOBATCHSUPPORT).setDescription(getString(R.string.warning_nobatching));
        getCardStream().showCard(CARD_NOBATCHSUPPORT);
        Log.w(TAG, "Could not register sensor listener in batch mode, " + "falling back to continuous mode.");
    }

    if (maxdelay > 0 && batchMode) {
        // Batch mode was enabled successfully, show a description card
        getCardStream().showCard(CARD_BATCHING_DESCRIPTION);
    }

    // Show the explanation card
    getCardStream().showCard(CARD_EXPLANATION);

    // END_INCLUDE(register)

}

From source file:io.github.msc42.masterthemaze.GameActivity.java

@Override
protected void onPause() {
    super.onPause();

    if (mGameThread != null) {
        mGameThread.pause();/*  w  w w.  j  a v  a  2s  .  c  o  m*/
    }

    if (mAddCurrentDirectionToQueueThread != null) {
        mAddCurrentDirectionToQueueThread.terminate();
    }

    if (mMotion) {
        SensorManager sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        Sensor accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        sensorManager.unregisterListener(mAccelerometerEventListener, accelerometer);
    }

    if (mMotion) {
        mMotionDescriptionTextView.setText(R.string.pause);
    } else {
        mTouchDescriptionTextView.setText(R.string.pause);
        mUpButton.setVisibility(View.INVISIBLE);
        mRightButton.setVisibility(View.INVISIBLE);
        mDownButton.setVisibility(View.INVISIBLE);
        mLeftButton.setVisibility(View.INVISIBLE);
    }
}

From source file:io.github.msc42.masterthemaze.GameActivity.java

@Override
protected void onResume() {
    super.onResume();

    if (mGameThread != null) {
        mGameThread.restart();//from  ww w. j av a2s.c  o m
    }

    if (mMotion) {
        if (mSpeed > 0) {
            mAddCurrentDirectionToQueueThread = new AddCurrentDirectionToQueueThread(mMotionQueue,
                    mCurrentMoveDirection, mSpeed);
            mAddCurrentDirectionToQueueThread.start();
        }

        SensorManager sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        Sensor accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        mAccelerometerEventListener = new AccelerometerEventListener(mRotation, mSensitivity,
                mCurrentMoveDirection, mMotionQueue);

        sensorManager.registerListener(mAccelerometerEventListener, accelerometer,
                SensorManager.SENSOR_DELAY_GAME, mSensorHandler);
    }

    if (mMotion) {
        mMotionDescriptionTextView.setText(R.string.connecting);
    } else {
        mTouchDescriptionTextView.setText(R.string.connecting);
        mUpButton.setVisibility(View.INVISIBLE);
        mRightButton.setVisibility(View.INVISIBLE);
        mDownButton.setVisibility(View.INVISIBLE);
        mLeftButton.setVisibility(View.INVISIBLE);
    }
}

From source file:ngoc.com.pedometer.ui.Fragment_Overview.java

private void startCountOrPause() {
    SensorManager sm = (SensorManager) getActivity().getSystemService(Context.SENSOR_SERVICE);
    if (getActivity().getSharedPreferences("pedometer", Context.MODE_PRIVATE).contains("pauseCount")) { // currently paused -> now resumed
        sm.registerListener(this, sm.getDefaultSensor(Sensor.TYPE_STEP_COUNTER), SensorManager.SENSOR_DELAY_UI,
                0);/*  ww  w.  jav  a 2  s .  co m*/
    } else {
        sm.unregisterListener(this);
    }
    getActivity().startService(
            new Intent(getActivity(), SensorListener.class).putExtra("action", SensorListener.ACTION_PAUSE));
}

From source file:org.zywx.wbpalmstar.plugin.uexaudio.EUExAudio.java

public void setProximityState(String[] params) {
    if (params == null || params.length < 1)
        return;//from  w  w  w. j  a v  a2s . co  m
    boolean state = "1".equals(params[0]);
    SensorManager mSensorManager = (SensorManager) mContext.getApplicationContext()
            .getSystemService(Context.SENSOR_SERVICE);
    if (!state) {
        if (sensorEventListener != null)
            mSensorManager.unregisterListener(sensorEventListener);
        return;
    }
    Sensor mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
    if (sensorEventListener == null) {
        if (m_pfMusicPlayer != null) {
            sensorEventListener = m_pfMusicPlayer.getSensorEventListener();
        } else {
            errorCallback(0, EUExCallback.F_E_AUDIO_MUSIC_STOP_NO_OPEN_ERROR_CODE,
                    /* "" */finder.getString(mContext, "plugin_audio_no_open_error"));
            return;
        }
    }
    mSensorManager.registerListener(sensorEventListener, mSensor, SensorManager.SENSOR_DELAY_NORMAL);
}

From source file:com.achep.acdisplay.services.activemode.sensors.ProximitySensor.java

@Override
public void onStart(@NonNull SensorManager sensorManager) {
    if (DEBUG)// w w  w  .  j  av  a2  s . c  om
        Log.d(TAG, "Starting proximity sensor...");

    mHistory.clear();
    mHistory.add(new Event(false, getTimeNow()));

    Config.getInstance().registerListener(this);
    updateWave2WakeProgram();

    // Ignore pocket program's start delay,
    // so app can act just after it has started.
    mFirstChange = true;
    mPocketProgram.dataArray[0].timeMin = 0;

    Sensor proximitySensor = sensorManager.getDefaultSensor(getType());
    sensorManager.registerListener(this, proximitySensor, SensorManager.SENSOR_DELAY_NORMAL);

    mMaximumRange = proximitySensor.getMaximumRange();
    sAttached = true;
}

From source file:org.jitsi.android.gui.call.ProximitySensorFragment.java

/**
 * {@inheritDoc}// w  w w .j  av a 2  s .c om
 */
@Override
public void onResume() {
    super.onResume();

    SensorManager manager = JitsiApplication.getSensorManager();

    // Skips if the sensor has been already attached
    if (proximitySensor != null) {
        // Re-registers the listener as it might have been
        // unregistered in onPause()
        manager.registerListener(this, proximitySensor, SensorManager.SENSOR_DELAY_UI);
        return;
    }

    List<Sensor> sensors = manager.getSensorList(Sensor.TYPE_ALL);
    logger.trace("Device has " + sensors.size() + " sensors");
    for (Sensor s : sensors) {
        logger.trace("Sensor " + s.getName() + " type: " + s.getType());
    }

    this.proximitySensor = manager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
    if (proximitySensor == null) {
        return;
    }

    logger.info("Using proximity sensor: " + proximitySensor.getName());
    sensorDisabled = false;
    manager.registerListener(this, proximitySensor, SensorManager.SENSOR_DELAY_UI);
}